How to remove an specific array from matrix

1 view (last 30 days)
Amy Xu
Amy Xu on 16 Apr 2017
Answered: Roger Stafford on 16 Apr 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
];

Answers (2)

Roger Stafford
Roger Stafford on 16 Apr 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 on 16 Apr 2017
Edited: Stephen23 on 16 Apr 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

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!