How to Add Multiple Arrays?

I am new to matlab. I want to do the following: I had created nine arrays and gave each one a name from A to I, said: A,B,C,D,E,F,G,H,I. Once I done that what I want to do is add them in groups of three and put to each new array the name of the last array added. For example. array C=A+B+C, array F=D+E+F, array I=G+H+I, I want to do this using a for loop which I think will be the most practical way of do it.
Thank you

 Respuesta aceptada

Oleg Komarov
Oleg Komarov el 19 de Mzo. de 2011

2 votos

If you could know how many matrices you have in advance (num), then preallocate before the loop:
dir1='C:\Documents and Settings\ann\My Documents\MATLAB\AMSR-E';
A = zeros(586,1383,num);
c = 1;
for year=2010:2010
for month=10:12
for day=1:eomday(year,month)
dd = [num2str(year),'.',num2str(month,'%0.2d'),...
'.',num2str(day,'%0.2d')];
file1 = ls([dir1,'\',dd,'\*.hdf']);
A(:,:,c) = hdfread([dir1,'\',dd,'\',file1],...
'Ascending_Land_Grid',...
'Fields', 'A_Veg_Water_Content');
% Increase counter
c = c+1;
end
end
end
You'll end up with a 3d array.
EDIT
Then to consolidate as Sean suggested use sum(A,3) in a loop:
A = rand(586,1383,8);
% Number of days to consolidate each time
n = 3;
% Final amount of slices
sl = ceil(size(A,3)/n);
% Preallocate
B = zeros(586,1383,sl);
for s = 1:sl-1
B(:,:,s) = sum(A(:,:, (s-1)*n+1:s*n ), 3);
end
% Final slice can be the result of less than 3 days
B(:,:,s+1) = sum(A(:,:,s*n+1:end),3);
Oleg

3 comentarios

Doralee
Doralee el 19 de Mzo. de 2011
Oleg,
Thank you. yes, I could know how many matrices I have in advance, I will start try this and look forward for your solution.
thank you
Doralee
Doralee el 19 de Mzo. de 2011
Oleg,
THANK YOU!
I will implement this solution, I think it will work.
Thank you for your time I really appreciated.
Oleg Komarov
Oleg Komarov el 19 de Mzo. de 2011
If you have NaNs then you should consider how to treat them, otherways they'll propagate causing loss of info.

Iniciar sesión para comentar.

Más respuestas (2)

Andrew Newell
Andrew Newell el 19 de Mzo. de 2011

0 votos

It's not clear to me why you want loops. As long as the arrays are the same size, you can just enter the equations the way you wrote them above, e.g.,
C = A+B+C
and it will work.

9 comentarios

Doralee
Doralee el 19 de Mzo. de 2011
Andrew,
Thank you for your answer. The Problem is, that i do need a Loop because I said that I had nine matrices just to make the example simple, But what I have to deal with are Thousands of matrices and it will not be practical to do it manually like you suggest.
Thank you,
Angel W
Doralee
Doralee el 19 de Mzo. de 2011
Can you still help me?
Andrew Newell
Andrew Newell el 19 de Mzo. de 2011
So how are all these matrices named? You'll run out of letters pretty fast.
Andrew Newell
Andrew Newell el 19 de Mzo. de 2011
And how are you generating them? Are you reading them from a file or getting them from some other calculation?
Doralee
Doralee el 19 de Mzo. de 2011
Andrew,
yes, I think I am not explaining well what I am doing. What I am doing is reading satellite data. Satellite data are organized by years. Each matrix is named with their respective date the satellite collected the data. For example 2010.01.13, 2010.01.14, 2010.01.15....and so on. I already wrote a code to read the data from the file. But now what i need to do is organize the data every 3 days. For example, add these three days 2010.01.01+2010.01.02+2010.01.03 and the put the name 2010.01.03 to that calculation.
what do you think?
Thank you!
Oleg Komarov
Oleg Komarov el 19 de Mzo. de 2011
You should use a cell array or a structure.
If the satellite data for each day is a vector/matrix you could combine it into a single double matrix/3d array (padding with NaN where necessary). That "could" be optimal.
In order to help you we should start from the format of the data as it is stored in files.
Doralee
Doralee el 19 de Mzo. de 2011
...I have to repeat that same pattern for many years of data, each matrix has 1,383 columns and 586 rows and all the matrices are of the same size.
Thank you
Doralee
Doralee el 19 de Mzo. de 2011
I am really new to matlab and don't know how to manage all the functions, Satellite data is stored in HDF format. matlab has a function to read satellite data format that function is hdfread. here is the code I wrote for extract the data from the original files:
dir1='C:\Documents and Settings\ann\My Documents\MATLAB\AMSR-E';
for year=2010:2010
for month=10:12
for day=1:eomday(year,month)
dd = [num2str(year),'.',num2str(month,'%0.2d'),'.',num2str(day,'%0.2d')];
file1 = ls([dir1,'\',dd,'\*.hdf']);
A = hdfread([dir1,'\',dd,'\',file1], 'Ascending_Land_Grid', 'Fields', 'A_Veg_Water_Content');
eval(['Data_Ascending_' num2str(year), num2str(month,'%0.2d'),num2str(day,'%0.2d') '=A']);
end
end
end
Doralee
Doralee el 19 de Mzo. de 2011
Oleg Komarov,
Thank you for your answer. How do I do that? could you give me an example?
Thank you a lot!

Iniciar sesión para comentar.

Sean de Wolski
Sean de Wolski el 19 de Mzo. de 2011

0 votos

doc cat
doc sum

2 comentarios

Doralee
Doralee el 19 de Mzo. de 2011
Sean de,
Thank you for your answer.I just read about functions cat and sum, but for example once I applied cat function how do I make matlab understand that I want to add columns alternatively.
thank you
Matt Tearle
Matt Tearle el 19 de Mzo. de 2011
When you say "alternatively", do you mean "columns rather than rows" or "alternating columns"?
Former: sum(A,2)
Latter: sum(A(:,1:2:end))

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 19 de Mzo. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by