フィルターのクリア

How to Sum Certain Values in Matrix A Based on Values of Matrix B in a Loop

2 ビュー (過去 30 日間)
Andrew
Andrew 2013 年 9 月 1 日
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
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
Andrew 2013 年 9 月 1 日
I've edited my question to make that clearer.

サインインしてコメントする。

採用された回答

kjetil87
kjetil87 2013 年 9 月 1 日
編集済み: kjetil87 2013 年 9 月 1 日
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
  1 件のコメント
Andrew
Andrew 2013 年 9 月 1 日
Thank you so much! This seems to be exactly what I need- I will let you know if that isn't the case.
I actually mistyped before: for the range [0,3], I do want to add up the value that corresponds to 0, 1, 2 and 3. Before, I had written only that I wanted to sum the values corresponding at 0,1,2. I apologize.
Also files is just the collection of .nc files I am reading into my loop, and length(files) is 15, the number of files that are being read in.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by