Data matrix manipulation in Matlab
4 views (last 30 days)
Show older comments
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 Comments
the cyclist
on 12 Mar 2015
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]
?
Answers (2)
Image Analyst
on 12 Mar 2015
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);
0 Comments
Stephen23
on 12 Mar 2015
Edited: Stephen23
on 12 Mar 2015
>> 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.
0 Comments
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!