how to fill matrix in a specific way

3 次查看(过去 30 天)
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

采纳的回答

KSSV
KSSV 2017-3-16
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by