How to remove an specific array from matrix

1 visualización (últimos 30 días)
Amy Xu
Amy Xu el 16 de Abr. de 2017
Respondida: Roger Stafford el 16 de Abr. de 2017
Matrix A as follows:
A = [
1 2 0 0 0
2 100 5 100 1
36 12 25 100 0
8 5 100 0 0
9 1 2 5 9
1 2 3 89 100
];
I want to remove any array which is equal to 100 from matrix A and form matrix L:
L = [
1 2 0 0 0
2 5 1 0 0
36 12 25 0 0
8 5 0 0 0
9 1 2 5 9
1 2 3 89 0
];

Respuestas (2)

Roger Stafford
Roger Stafford el 16 de Abr. de 2017
[m,n] = size(A);
for k = 1:m
a = A(k,A(k,:)~=100);
A(k,:) = [a,zeros(1,n-length(a))];
end

Stephen23
Stephen23 el 16 de Abr. de 2017
Editada: Stephen23 el 16 de Abr. de 2017
>> A = [1,2,0,0,0;2,100,5,100,1;36,12,25,100,0;8,5,100,0,0;9,1,2,5,9;1,2,3,89,100];
>> L = A;
>> L(L==100) = 0;
>> L = cell2mat(cellfun(@(v)[v(v~=0),v(v==0)],num2cell(L,2),'uni',0))
L =
1 2 0 0 0
2 5 1 0 0
36 12 25 0 0
8 5 0 0 0
9 1 2 5 9
1 2 3 89 0

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by