Variable Conversion
조회 수: 1 (최근 30일)
이전 댓글 표시
for i=1:10
A=zeros(100,4);
A(:,1)=randi(100,100,1) %Course(1)
A(:,2)=randi(5,100,1) %Day(2)
A(:,3)=randi(10,100,1) %Timeslot(3)
A(:,4)=randi(17,100,1) %Room(4)
chr(:,:,i)=A
end
Referring to the code snippet above, may I know if there is a way to convert chr(:,:,i) into a simple variable,say A(i) where i is the loop index)? The output has to be similar to chr(:,:,i).
댓글 수: 0
채택된 답변
the cyclist
2011년 10월 23일
If I am correctly interpreting what it is you want to do, then I think the best way is to use "cell arrays". Here is an example that is like yours:
A = cell(10,1);
for i=1:10
A{i}=zeros(100,4);
A{i}(:,1)=randi(100,100,1); %Course(1)
A{i}(:,2)=randi(5,100,1); %Day(2)
A{i}(:,3)=randi(10,100,1); %Timeslot(3)
A{i}(:,4)=randi(17,100,1); %Room(4)
end
Now each element of the cell array, for example A{1}, is the "simple variable" you want. It is like naming them A1, A2, etc, only better.
댓글 수: 3
the cyclist
2011년 10월 23일
You might want to start a fresh question, rather than tucking this in the comments. More people will see it. Your question is a bit vague, but maybe this will be helpful. Think of a cell array A as a container, where each element A{i} can be operated on in the usual way. For example, uniqueX{i} = unique(X{i}) would operate exactly like that other statement did. It can be a bit tricky to get used to the notation. Specifically, A(i) refers to the cell, but A{i} refers to the CONTENTS of the cell (e.g. the array).
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!