Borrar filtros
Borrar filtros

Reshape matrix with zero value

3 visualizaciones (últimos 30 días)
Francesco
Francesco el 22 de Feb. de 2014
Respondida: Andrei Bobrov el 22 de Feb. de 2014
I have this variable formatted as following:
>> dist
dist =
0 7.5000 15.0000 16.7705 21.2132 16.7705 15.0000 7.5000
7.5000 0 7.5000 10.6066 16.7705 15.0000 16.7705 10.6066
15.0000 7.5000 0 7.5000 15.0000 16.7705 21.2132 16.7705
16.7705 10.6066 7.5000 0 7.5000 10.6066 16.7705 15.0000
21.2132 16.7705 15.0000 7.5000 0 7.5000 15.0000 16.7705
16.7705 15.0000 16.7705 10.6066 7.5000 0 7.5000 10.6066
15.0000 16.7705 21.2132 16.7705 15.0000 7.5000 0 7.5000
7.5000 10.6066 16.7705 15.0000 16.7705 10.6066 7.5000 0
I have just posted this question before but I'm not quite clear in the explatation. I want to obtain a 8x7 matrix because I want to eliminate all the 0 element rows by rows. How can I do this?
It should be a matrix like this:
7.5000 15.0000 16.7705 21.2132 16.7705 15.0000 7.5000
7.5000 7.5000 10.6066 16.7705 15.0000 16.7705 10.6066
15.0000 7.5000 7.5000 15.0000 16.7705 21.2132 16.7705
and so on.
Is it possible to do this thing?

Respuesta aceptada

Mischa Kim
Mischa Kim el 22 de Feb. de 2014
Looks like you just want to remove the diagonal elements. Check out this answer.
  2 comentarios
Francesco
Francesco el 22 de Feb. de 2014
Do you think that this code is good?
function Z=diagrem(X)
N=size(X);
Z=X;
if N(1)~=N(2)
error('Matrix is not square');
end
for x=1:N(1)
Z(x,x)=0;
end
for y=1:N(1)-1
Z(y,y+1)=0;
end
Z(Z==0)=[];
Z=reshape(Z,N(1)-1,N(2)-1);
The problem is that MATLAB said:
??? function Z=diagrem(dist) | Error: Function definitions are not permitted at the prompt or in scripts.
Francesco
Francesco el 22 de Feb. de 2014
b=triu(a)
b=b(:,2:end)
c=tril(a)
c=c(:,1:end-1)
b+c
This code seemns to work fine. Do you think so?

Iniciar sesión para comentar.

Más respuestas (1)

Andrei Bobrov
Andrei Bobrov el 22 de Feb. de 2014
n = size(dist1);
out = zeros(n-[1 0]);
out(:)=dist(~eye(n));
out = out';

Categorías

Más información sobre Creating and Concatenating 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