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

조회 수: 2 (최근 30일)
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 CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by