How convert matrix to original form
조회 수: 2 (최근 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에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!