필터 지우기
필터 지우기

Data matrix manipulation in Matlab

조회 수: 4 (최근 30일)
Jeong Ho
Jeong Ho 2015년 3월 12일
편집: Stephen23 2015년 3월 12일
Hi, I have a data matrix, and each row is (t, i, row_vector), i.e., the first two elements represent time t and individual i, and the rest is a row vector of information on (t,i). For simplicity, assume row_vector is a real number. I want to create a matrix M s.t. M(t,i) = row_vector (i.e., the real number data on (t,i)) and M(t,i) missing if (t,i) does not turn up in the original data matrix. How do I do this? I'm lost how to do this.. I'd really appreciate any and all help. Thank you very much in advance!
Best, John
  댓글 수: 2
the cyclist
the cyclist 2015년 3월 12일
Suppose your input data matrix is
data = [1 3 4 5 6;
4 2 7 8 9];
What do you want your output to be?
Do you want it to be a cell array where
M{1,3} = [4 5 6]
and
M{4,2} = [7 8 9]
?
Jeong Ho
Jeong Ho 2015년 3월 12일
Dear the cyclist, Yes, that's exactly it! Is there a way of doing it?

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

답변 (2개)

Image Analyst
Image Analyst 2015년 3월 12일
Try this:
data = randi(50, 10, 7)
[rows, column] = size(data);
for row = 1 : rows
t = data(row, 1);
i = data(row, 3);
M{t, i} = data(row, 3:end);
end
% Print to command window:
celldisp(M);

Stephen23
Stephen23 2015년 3월 12일
편집: Stephen23 2015년 3월 12일
This can also be solved very neatly using accumarray:
>> data = [1 3 4 5 6; 4 2 7 8 9];
>> A = repmat(data(:,1:2),size(data,2)-2,[]);
>> B = reshape(data(:,3:end),[],1);
>> C = accumarray(A,B,[],@(v){v.'})
C =
[] [] [1x3 double]
[] [] []
[] [] []
[] [1x3 double] []
>> C{1,3}
ans =
6 5 4
>> C{4,2}
ans =
7 8 9
This is likely to scale better to larger matrices than using a loop.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by