How to Sum Certain Values in Matrix A Based on Values of Matrix B in a Loop
显示 更早的评论
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 个评论
Azzi Abdelmalek
2013-9-1
编辑:Azzi Abdelmalek
2013-9-1
In your example you gave A and B, then you are referring to C and D, this is not clear
Andrew
2013-9-1
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Signal Attributes and Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!