How to Sum Certain Values in Matrix A Based on Values of Matrix B in a Loop
2 views (last 30 days)
Show older comments
Hello,
I have a 16 x 24 matrix A composed of one row of the hours 0,1,2...23 (the first row), and in the rest of the rows, I have hourly wind powers, in which each row corresponds to the wind speeds in a different location in the world.
What I am trying to do is use matrix B of size 15 x 2 to specify start and end times in matrix A in a loop, and then add up the wind powers in each row between those times, inclusive, and then store each of those sums as an entry in a 15 x 1 matrix. Each row of B has different start and end times.
I will illustrate with a example:
A = [0 1 2 3 4 5...
5 6 7 8 9 2...]
B = [0 3];
size(A) = 2 x 5;
size(B) = 1 x 2;
So I want to add up 5, 6, 7, and 8 to get 26, and store that value in a new column matrix. I want to do this for each row of A and B, but for different start and end values, depending on the row of B.
What I've done so far is this:
%%G is equivalent to matrix A above; Rounded_Bounds_WS is equivalent to matrix B above; J is the column matrix I'd like to create.
for s=1:length(files) %files is just the collection of .nc files I'm using (15)
if and(G(1,:)>= Rounded_Bounds_WS(s,1), G(1,:)<= Rounded_Bounds_WS(s,2))
J(s,:)=sum(G(s+1,:));
end
end
But I haven't been able to get this to work. s cycles from 1 to 15 (where 15 =length(files); however the matrix J isn't even created.
Thank you in advance for your help.
Andrew
[edited for clarity -- the cyclist]
2 Comments
Azzi Abdelmalek
on 1 Sep 2013
Edited: Azzi Abdelmalek
on 1 Sep 2013
In your example you gave A and B, then you are referring to C and D, this is not clear
Accepted Answer
kjetil87
on 1 Sep 2013
Edited: kjetil87
on 1 Sep 2013
Im not a 100% sure if this is what you asked for but ill have a go at it =)
A=[0:23;... %hours 0 to 23
rand(15,24)]; %15x24 submatrix with random values.
B=randi([0,23],15,2)]; % 15x2 matrix with numbers between 0 and 23
B=sort(B,2); % now the first value in each row is the lowest.
Im not sure what "files" and length(files) is, but ill make an example based on your first A and B.
J=zeros(size(B,1)); %pre allocate J to avoid having to reallocate during loop.
for i=1:size(B,1)
J(:,i)=sum( A(2:end,(B(i,1):B(i,2))+1),2); % sum each row in a from B(i,1)
% to B(i,2).
% add one to the index since
% "hour 0" in A
% is located in column 0.
end
Is this what you wanted? If e.g B was equal to
B=[0,6;
..
..
..
J(1,1) will contain sum( A (2, 1:7) ) . While J(2,1) contains sum( A (3,1:7) ) and so forth.
To realete to your first example:
J=[];
A = [0 1 2 3 4 5;
5 6 7 8 9 2];
B = [0 3];
J(:,1)=sum( A(2:end , (B(1,1):B(1,2))+1 ,2)
J =
26
Note that the sum is 26, because i assume that when B is [0,3] you want to use colums of A that contains 0 , 1 ,2 AND 3. If you only want 0 1 2 remove
(B(i,1):B(i,2))+1
and replace it with
B(i,1)+1:B(i,2)
In your specific example:
sum( A(2:end , B(1,1)+1:B(1,2) ) ,2)
ans =
18
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!