How to create multiple blank matrices using loop ?

조회 수: 9 (최근 30일)
Sachin Sreedharan
Sachin Sreedharan 2019년 1월 23일
편집: Stephen23 2019년 1월 23일
I Need to create multiple blank matrices so that i can extract data onto the matices and use it for later use. I cant find comands to create multiple matrices of different names names being A1,A2,A3 etc.
  댓글 수: 1
Stephen23
Stephen23 2019년 1월 23일
편집: Stephen23 2019년 1월 23일
"...create multiple matrices of different names names being A1,A2,A3 etc. "
Dynamically creating variable names like that is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. The MATLAB documentation specifically recommends against magically creating variable names like that: "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."
Note that forcing a number into the variable name is treating that number as a pseudo-index, which all experienced users know is much better written as a real index into one variable. Real indexing is neat, simple, very fast, efficient, and much simpler to debug, and will make your MATLAB code simpler and more efficient. Unlike what you are trying to do.
Read this to know more:
"I Need to create multiple blank matrices"
MATLAB does not have a concept of "blank matrices": all matrices contain data. However it is certainly easy to preallocate matrices:

댓글을 달려면 로그인하십시오.

답변 (1개)

KSSV
KSSV 2019년 1월 23일
YOu need not define A1,A2 A3 etc.....you need to initialize a 3D matrix. For EXample:
A = rand(2,2,3) ;
A1 = A(:,:,1) ;
A2 = A(:,:,2) ;
A3 = A(:,:,3) ;
  댓글 수: 2
Sachin Sreedharan
Sachin Sreedharan 2019년 1월 23일
Still this is defining. I need blank matrices from loop. The matrice name changing with every iteration. 1st iteration i ll have A1=[ ], 2nd iteration A2= [ ], ... and so on upto maybe A12= [ ].
i tried for loop mixed with sprintf
for m=1:12
sprintf(''A%d',m)=[];
end
Stephen23
Stephen23 2019년 1월 23일
편집: Stephen23 2019년 1월 23일
"The matrice name changing with every iteration"
Magically changing variable names is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code. Do not do this if you want to learn how to use MATLAB effectively and actually spend your time on more useful things than chasing down pointless bugs.
The best solution is probably to use one ND array, or possibly one cell array, with some simple and efficient indexing. For example, you could use a cell array:
A = cell(1,12);
Note how this is already simpler than what you were attempting to do in a loop!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by