How to remove an specific array from matrix

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

回答(2 个)

Roger Stafford
Roger Stafford 2017-4-16
[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 2017-4-16
编辑:Stephen23 2017-4-16
>> 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

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by