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 Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by