Indexing Based On Cell Array

조회 수: 4 (최근 30일)
RDG
RDG 2012년 9월 6일
randvar=cell(50,1);
for h=1:50
randvar{h}=zeros(10,4);
randvar{h}(:,1)=randi(10,10,1);
randvar{h}(:,2)=randi(5,10,1);
randvar{h}(:,3)=randi(10,10,1);
randvar{h}(:,4)=randi(17,10,1);
end
for counter=1:15
for h=1:50
a{h}=0;
for i=1:10
for j=1:10-i
if randvar{h}(i,1) ~= randvar{h}(i+j,1)
if randvar{h}(i,2)==randvar{h}(i+j,2)
if randvar{h}(i,3)==randvar{h}(i+j,3)
if randvar{h}(i,4)==randvar{h}(i+j,4)
a{h}=a{h}+10;
else
a{h}=a{h}+0;
end
end
end
end
end
end
end
end
[B, IX] = sortrows(a);
least = randvar(IX(1:10));
I don't quite understand why the IX returns 1. Shouldn't it return all the index of the cell array elements? I want to retrieve the first 10 least value for a for randvar.
P/S: My code works when I'm using cell2mat but because there're many loops, converting from cell2mat and mat2cell doesn't seem to be efficient.
  댓글 수: 1
Andrei Bobrov
Andrei Bobrov 2012년 9월 6일
편집: Andrei Bobrov 2012년 9월 6일
a = cell(size(randvar));
for jj = 1:numel(a)
x = randvar{jj};
[b b c] = unique(x(:,1),'first');
y = false(size(c));
y(b) = true;
z = x(y,2:end);
[b b c] = unique(z,'first','rows');
q = true(size(c));
q(b) = false;
a{jj} = nnz(q)*10;
end
[IX, IX] = sortrows(a);
least = randvar(IX(1:10));
or code for loop for..end
for h=1:50
a{h}=0;
for i=1:10
for j=1:10-i
if randvar{h}(i,1) ~= randvar{h}(i+j,1)&&...
isequal(randvar{h}(i,2:end),randvar{h}(i+j,2:end))
a{h}=a{h}+10;
end
end
end
end

댓글을 달려면 로그인하십시오.

채택된 답변

Arthur
Arthur 2012년 9월 6일
편집: Arthur 2012년 9월 6일
a is an matrix of 1 row and 50 columns. And like doc sortrow states: "If A is an m-by-n matrix, then B = A(index,:)."
So, initialize a like this, and you're fine:
a = cell(50,1);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by