Filter löschen
Filter löschen

Variable Conversion

1 Ansicht (letzte 30 Tage)
RDG
RDG am 23 Okt. 2011
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).

Akzeptierte Antwort

the cyclist
the cyclist am 23 Okt. 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 Kommentare
RDG
RDG am 23 Okt. 2011
If I may ask further, referring to the previous code which you gave me,
X = [1 2 3 4 5 5 5 1];
uniqueX = unique(X);
countOfX = hist(X,uniqueX);
indexToRepeatedValue = (countOfX~=1);
repeatedValues = uniqueX(indexToRepeatedValue)
numberOfAppearancesOfRepeatedValues = countOfX(indexToRepeatedValue)
Now that I'm using cell array to represent my variable, how do I denote it in the form of uniqueX = unique(X)? I tried a few ways but I got some "Dimension Error" message.
P/S I read from your profile that you're a researcher? A pleasure to meet you!
the cyclist
the cyclist am 23 Okt. 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).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by