how to fill matrix in a specific way

3 visualizaciones (últimos 30 días)
lucksBi
lucksBi el 16 de Mzo. de 2017
Comentada: lucksBi el 16 de Mzo. de 2017
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

Respuesta aceptada

KSSV
KSSV el 16 de Mzo. de 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

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by