필터 지우기
필터 지우기

Dynamic Matrices in Matlab

조회 수: 3 (최근 30일)
MiauMiau
MiauMiau 2012년 12월 13일
Hi!
Yesterday, I had a very similar question, now I want something a bit more advanced. So I have coded already:
data = [234,3,8;457,5,7;611,7,3;234,9,12]
ID = data(:,1);
X = data(:,2);
U = unique(ID)
data(U(i) == data(:,1),3)
display data
Now I want to create a matrix, where the first column would be the first column of the data matrix above (i.e. "id"). And all the following collumns would then store the values in the third column of data belonging to this id. So it would look like:
[234, 8,12; 457, 7,0; --- ]
But, obviously, I could not set the dimensions of this matrix from the beginning on, the number of columns would have to be dynamical. Is that possible?
Thanks
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2012년 12월 13일
It's not clear
MiauMiau
MiauMiau 2012년 12월 13일
My question is not clear? So, I would like to have a matrix as follows:
[ 234,8,12; 457,7,0; 611,3,0]
the first column would be the id - and the following columns of the same row would be the third elements in the data matrix belonging to this id. As in the example, it is not clear, how many third column elements each id has - so the number of columns of the matrix would have to be dynamical. Is that possible to programm in matlab?

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

답변 (2개)

Andrei Bobrov
Andrei Bobrov 2012년 12월 13일
편집: Andrei Bobrov 2012년 12월 13일
[a,b,c] = unique(data(:,1));
d = accumarray(c,data(:,3),[],@(x){sort(x)});
n = cellfun(@numel,d);
m = numel(a);
out = [a, zeros(m,max(n))];
for j1 = 1:m
out(j1,2:n(j1)+1) = d{j1};
end
  댓글 수: 2
Andrei Bobrov
Andrei Bobrov 2012년 12월 13일
corrected
MiauMiau
MiauMiau 2012년 12월 13일
the output I get is:
>> test
data =
234 3 8
457 5 7
611 7 3
234 9 12
so the data matrix itself...

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


Walter Roberson
Walter Roberson 2012년 12월 13일
Is the number of matching elements possibly different between the id's? If it is, then you cannot do this with a numeric matrix and need to use a cell array instead.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by