Variable Conversion
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
the cyclist
le 23 Oct 2011
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 commentaires
the cyclist
le 23 Oct 2011
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).
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!