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 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by