MATLAB Answers

Sum specific values from a matrix

8 views (last 30 days)
hello
I have a matrix existing of 2 colums and a variable number of rows. the first colum is "amplitude" the second colum is "frequency". for some frequencys in a specific range i want to change the amplitude. so for example: for frequencys from 5 to 8 the amplitude has to be multiplied by 2. for frequencys from 9 to 14 the amplitude needs to be multiplied by 4 and so on. for that i used if loops in a for loop as seen in the code below. now for each range of frequency i want the amplitudes to be added. so for frequencys 4 to 8, all the amplitudes needs to be multiplied by 2 and then al these amplitudes need to be added to each other and saved in a new variable. can someone help me to solve my problem.
thanks in advance
treshold1 = 0.0001; % treshold amplitude
treshold2 = 0; %treshold freq
%K(K(:,1)<treshold1,:)=[0];
K(K(:,2)<treshold2,:)=[0];
for i = 1:size(K,1)
if K(i,2) >=5 && K(i,2)<9
K(i,1)*2
%K1 = sum of the amplitude for freq 5 to 8
if K(i,2) >=9 && K(i,2)<15
K(i,1)*4
%K2 = sum of the amplitude for freq 9 to 14
end
end

  0 Comments

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 24 Feb 2020
"What im trying to do is to add all the amplitudes off column 1 for a specific frequency range"
This is a different question from what you originally asked, so would have been better started as a new question.
Anyway, for this you'd use discretize to assign unique numbers to each frequency band and splitapply to sum all rows that belong to a given band:
frequencybands = [0, 24, 51.5, 80, Inf]; %Creates 4 frequency bands: [0, 24), [24, 51.5), [51.5, 80), [80, Inf]
bandnumber = discretize(K(:, 2), frequencybands); %assign unique band number to members of a band
bandamplitude = splitapply(@sum, K(:, 1), bandnumber); %sum amplitudes that belong to the same band
Another option would be to convert your matrix to a table and then use groupsummary. A table also has the advantage of making it clearer what each column represent.
ampfreq = array2table(K, 'VariableNames', {'Amplitude', 'Frequency'});
frequencybands = [0, 24, 51.5, 80, Inf]; %Creates 4 frequency bands: [0, 24), [24, 51.5), [51.5, 80), [80, Inf]
bandamplitude = groupsummary(ampfreq, 'Frequency', freqbands, 'sum', 'Amplitude'); %sum amplitude according to frequency bands

  1 Comment

Sören Gevaert
Sören Gevaert on 24 Feb 2020
Hi Guillaume
Thanks for the help, this seems to work verry well. I expressed my first question a bit wrong. The first thing was to add the amplitudes and after that, these values needs to be multiplied with different factors. Now i can continue to work from this.
Kind regards

Sign in to comment.

More Answers (1)

Athul Prakash
Athul Prakash on 23 Feb 2020
Edited: Athul Prakash on 23 Feb 2020
You may use logical indexing as follows:
cond1 = K(:,2)>=5 & K(:,2)<9; %these are logical indices to select out your given ranges.
cond2 = K(:,2)>=5 & K(:,2)<9;
K(cond1,:) = K(cond1,:)*2;
K(cond2,:) = K(cond2,:)*4;
I'm not sure what you wish to do after this.
Hope it Helps!

  3 Comments

Guillaume
Guillaume on 24 Feb 2020
Comment by Sören Gevaert originally posted as an answer:
Hi Athul
So lets say that this is the matrix. the second column are the values of frequencys and the first column are the corresponding values of amplitude for each frequency. What im trying to do is to add all the amplitudes off column 1 for a specific frequency range. lets say for frequency range 1 to 24(red marker). considering that the number of elements for each frequency range can vary for a differint matrix: The values from column 1 coresponding with the frequency range need to be added up. so 4.9537e-17 + 9.7318e-17 + 0.2500 + 9.7632e-17 + ...... Im trying to figure out how to do this but i dont know how.
Thanks in advance.
Athul Prakash
Athul Prakash on 25 Feb 2020
For a closed interval [fMin fMax],
% suppose the matrix above is called 'M'
logical_indices = ( (M(:,2) >= fMin) & (M(:,2) <= fMax) );
amps_in_range = M(logical_indices, 1);
result = sum(amps_in_range);
You may compute a similar sum for all frequency bands.
Hope it Helps!
Sören Gevaert
Sören Gevaert on 25 Feb 2020
Hi Athul
This also seems to work verry well. It will also be verry usefull. Thanks for the help.
Kind regards

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by