Data matrix manipulation in Matlab
    5 次查看(过去 30 天)
  
       显示 更早的评论
    
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
      
      
 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]
?
回答(2 个)
  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);
0 个评论
  Stephen23
      
      
 2015-3-12
        
      编辑:Stephen23
      
      
 2015-3-12
  
      >> 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 个评论
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



