Saving a variable with an index in a for loop
조회 수: 6 (최근 30일)
이전 댓글 표시
Hello. I am running into an issue of saving a created matrix with an index added to it for example
for i=1:10
x=1:10
y=1:10
z=1:10
A=[x y z]
end
What im tryng to do is then save the resulting matrix as A1, A2, A3 ect. any pointers for this?
댓글 수: 0
답변 (1개)
Steven Lord
2021년 10월 4일
Can you do this? Yes.
Should you do this? The general consensus is no. See that Answers post for an explanation and alternatives.
댓글 수: 3
Walter Roberson
2021년 10월 4일
If you do not have consistency of sizes, then use a cell array
A{i} = [x y z]
Stephen23
2021년 10월 5일
편집: Stephen23
2021년 10월 5일
The solutions given above concatenate your data, which apparently your data is not suitable for.
You can trivially resolve this by using a cell array, for example:
c = cell(10,3);
for ii = 1:10
c{ii,1} = 1:10;
c{ii,2} = 1:10;
c{ii,3} = 1:10;
end
Your approach of putting pseudo-indices into the variable names should definitely be avoided.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!