make many matrices with for loop

Hi
I have problem with making matrices with for loop
I want to make 272 matrices by automatically. for example
Band1=[~~~] Band2=[~~~] Band3=[~~~] . . . Band272=[~~~]
here is my code that I made. and r= 2000, c=640 , b=272
a=geotiffread('D:\Junsu2\3rd semester\hangigong\tiff\13118.tif');
[r,c,b]= size(a);
for bandno=1:b
for i=1:r
for j=1:c
Band{bandno}(i,j)= a(i,j,bandno);
end
end
end
please help me I dont want to right 272times to make matrices.
Thank you

1 Comment

"I want to make 272 matrices by automatically"

While that is possible, in practice dynamically naming variables is how some beginners force themselves into writing slow, complex, buggy, and hard to debug code. The MATLAB documentation specifically advises against naming variables dynamically: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array." Read this to know more:

https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval

As the MATLAB advises, you should store the imported data in one array: that array might be a table, cell array, a matrix, or an ND array (which to choose depends on the data and what you want to do with it). Using one array makes looping over the files/data easy, makes accessing and processing the data very simple, and is much easier to debug (thus wasting less of your life debugging). Within the loop you will probably need to use indexing: indexing is simple, neat, and very efficient.

How to process a sequence of files is also explained in the MATLAB documentation:

https://www.mathworks.com/help/matlab/import_export/process-a-sequence-of-files.html

You should also read about the different file importing functions that can import text files:

https://www.mathworks.com/help/matlab/text-files.html

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 20 Apr 2018
Edited: Stephen23 on 20 Apr 2018

It is easy to split an MxNxP array into P separate arrays: just use num2cell. Note that MATLAB is a high-level language, so solving everything with lots of ugly loops like in low-level languages (like C++) is not required.

You just need to use num2cell like this:

a = geotiffread(...);
c = num2cell(a,1:2);

Each MxN matrix is in one cell of the cell array c. You can access them trivially and efficiently using cell array indexing:

c{1}
c{2}
c{3}
...

Personally I would not bother splitting up the data like this: the data is already stored in a perfectly good 3D numeric array, which you can access trivially and efficiently using indexing:

a = geotiffread(...);
for k = 1:size(a,3);
     a(:,:,k)
end

and also you can perform operations on the entire data using vectorized code. Also when you split this array into lots of matrices you create superfluous duplicate copies of the data in memory. In general it is better to keep data together as much as possible, rather than splitting it apart. I don't see any advantage in splitting up this array.

1 Comment

Thanks! Actually I wanted something like this. It was perfect answer for me.

Sign in to comment.

More Answers (0)

Asked:

on 20 Apr 2018

Commented:

on 21 Apr 2018

Community Treasure Hunt

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

Start Hunting!