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 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!