필터 지우기
필터 지우기

Variable Conversion

조회 수: 1 (최근 30일)
RDG
RDG 2011년 10월 23일
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).

채택된 답변

the cyclist
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
RDG
RDG 2011년 10월 23일
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 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 CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by