How to rearrange values in a matrix
Mostrar comentarios más antiguos
Hello, I have a large matrix with 3 columns(x,y,and z) and I want to rearrange the values inside this matrix. Example:
*Initial matrix
x y z
1 1 5
1 2 6
1 3 7
2 1 8
2 2 9
2 3 2
*Final matrix My goal is to rearrange the values of the matrix in this form
y
x 1 2 3
1 5 6 7 <-z values
2 8 9 2
Respuesta aceptada
Más respuestas (1)
Andrei Bobrov
el 4 de Nov. de 2013
Editada: Andrei Bobrov
el 4 de Nov. de 2013
xyz = [1 1 5
1 2 6
1 3 7
2 1 8
2 2 9
2 3 2];
out = nan(max(xyz(:,1:2))+1);
out(2:end,1) = unique(xyz(:,1));
out(1,2:end) = unique(xyz(:,2));
out(2:end,2:end) = accumarray(xyz(:,1:2),xyz(:,3));
ADD
x =[ 0.6000 0.6000 5.0000
0.6000 0.8000 6.0000
0.6000 0.9000 7.0000
0.8000 0.6000 8.0000
0.8000 0.8000 9.0000
0.8000 0.9000 2.0000];
[r,ii,ii] = unique(x(:,1));
[c,jj,jj] = unique(x(:,2));
v = accumarray([ii,jj],x(:,3));
out = [nan,c';r,v];
6 comentarios
afrya
el 4 de Nov. de 2013
afrya
el 3 de Abr. de 2014
Andrei Bobrov
el 3 de Abr. de 2014
x=[ 0.6000 0.6000 5.0000
0.6000 0.8000 6.0000
0.6000 0.8500 0
0.6000 0.8200 0
0.8000 0.9000 2.0000
0.8000 0.8000 9.0000];
[rc,~,c1] = arrayfun(@(ii)unique(x(:,ii)),1:2,'un',0);
v = accumarray([c1{:}],x(:,3));
out = [nan,rc{2}',nan;rc{1},v,sum(v,2)./sum(v>0,2)];
or other last row
out = [nan,rc{2}',nan;rc{1},v,sum(v,2)./numel(rc{2})];
afrya
el 5 de Abr. de 2014
Andrei Bobrov
el 5 de Abr. de 2014
another variant of last row
out = [nan,rc{2}',nan;rc{1},v,accumarray(c1{1},x(:,3),[],@mean)];
afrya
el 6 de Abr. de 2014
Categorías
Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!