how to fill matrix in a specific way
2 views (last 30 days)
Show older comments
Hi I have a matrix like this,
I want to take unique values from column 1 and 2 and sort them. Then create another matrix with rows and columns of sorted matrix and fill it with values in 3rd column of original matrix. like shown in attached image:
Thanks in advance
0 Comments
Accepted Answer
KSSV
on 16 Mar 2017
A = [11 44 1; 11 33 -1; 22 11 1; 22 44 1; 22 33 1;33 44 1; 33 22 -1; 44 11 1 ;
44 22 -1; 44 33 1] ;
c1 = A(:,1) ; c2 = A(:,2) ;
c1 = unique(c1) ;
c2 = unique(c2) ;
iwant = zeros(length(c1)+1,length(c2)+1) ;
iwant(2:end,1) = c1 ;
iwant(1,2:end) = c2 ;
for i = 2:size(iwant,1)
for j = 2:size(iwant,2)
[val,idx] = ismember([iwant(1,i) iwant(j,1)],A(:,1:2),'rows') ;
if idx~=0
iwant(i,j) = A(idx,3) ;
end
end
end
More Answers (0)
See Also
Categories
Find more on Shifting and Sorting Matrices 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!