How convert matrix to original form
1 次查看(过去 30 天)
显示 更早的评论
Hello I have a matrix with 100 rows and 100 columns named it D. in fact this matrix was a matrix with 137 rows and 127 columns but some rows and columns remove because every elements of them was zero. ok, matrix D is like and indexing matrix. let to explain it. the first row and first column of this matrix show the true number of row and column of elements whitin the matrix. I want to expand this matrix and change it to original form with aid of the first row and the first column. Is it possible to guide me? below is part of matrix D as example:
DD=[0 3 5 10 21 26 30;2 0 0 0.5 1 0 0;10 0 1 1 1 1 0;21 1 1 1 1 0.75 0;22 1 1 1 1 1 0.5;23 1 1 1 1 1 1;30 1 1 1 1 1 1;34 1 1 1 1 1 1;45 0 0 0 1 1 1;46 0 0 0 0 0 0;47 1 0.75 1 0.58 0.33 0.16;70 0.39 0.58 0.79 1 0 1;71 0 0 0.19 0 0 0;72 0 0 0.29 1 1 1;73 0.5 0 0 0 0 0]
Thank you.
0 个评论
回答(2 个)
jgg
2015-12-22
This loops, but it should be fast enough for your purposes, and it's easy to read, so you can probably figure out how to vectorize it easily enough.
r = max(DD(:,1));
c = max(DD(1,:));
rowinds = DD(:,1);
colinds = DD(1,:);
original = zeros(r,c);
for i = 2:size(DD,1)
for j = 2:size(DD,2)
original(rowinds(i),colinds(j)) = DD(i,j);
end
end
0 个评论
Walter Roberson
2015-12-22
rowinds = DD(2:end,1);
colinds = DD(1,2:end);
r = max(rowinds);
c = max(colinds);
original = zeros(r,c);
original(rowinds,colinds) = DD(2:end,2:end);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numeric Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!