Removing zeros from matrix

조회 수: 72 (최근 30일)
Robert Bridges
Robert Bridges 2021년 12월 7일
편집: Stephen23 2021년 12월 7일
Hi,
Been trying to remove these zeros using NaN and find etc. but to no avail.
Trying to remove zeros so that I can pull out the end (non-zero) values from each row and put into a new n:1 matrix. If there is also a way of doing this without nessisarily having to remove the zeros then that would be fine also.
Example matrix (mine is a lot larger but follows similar format with zeros being at the end of the rows):
A = [1 2 3 4 5 6; 1 2 3 4 5 6; 1 2 3 4 5 0; 1 2 3 4 0 0; 1 2 3 0 0 0]
Thanks in advance.
Edit:
The final desired output would be:
B = [6; 6; 5; 4; 3]

채택된 답변

Stephen23
Stephen23 2021년 12월 7일
편집: Stephen23 2021년 12월 7일
A = [1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,0;1,2,3,4,0,0;1,2,3,0,0,0]
A = 5×6
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 0 1 2 3 4 0 0 1 2 3 0 0 0
Method one: NONZEROS and CELLFUN
baz = @(v)v(end);
fnh = @(v)baz(nonzeros(v));
B = cellfun(fnh,num2cell(A,2))
B = 5×1
6 6 5 4 3
Method two: ROT90 and CUMSUM and logical indexing
tmp = rot90(A);
idx = tmp~=0;
idx = idx & cumsum(idx,1)==1;
B = tmp(idx)
B = 5×1
6 6 5 4 3
Method 3: FIND and CELLFUN
foo = @(v)v(find(v,1,'last'));
B = cellfun(foo,num2cell(A,2))
B = 5×1
6 6 5 4 3
Method 4: MAX and SUB2IND and linear indexing:
sza = size(A);
idc = max((A~=0).*(1:sza(2)),[],2);
idr = 1:sza(1);
B = A(sub2ind(sza,idr(:),idc))
B = 5×1
6 6 5 4 3

추가 답변 (2개)

Alan Stevens
Alan Stevens 2021년 12월 7일
Like this?
A = [1 2 3 4 5 6; 1 2 3 4 5 6; 1 2 3 4 5 0; 1 2 3 4 0 0; 1 2 3 0 0 0];
B = A'; B=B(:);
B(B==0)=[]
B = 24×1
1 2 3 4 5 6 1 2 3 4
  댓글 수: 3
Stephen23
Stephen23 2021년 12월 7일
Every row must have the same number of elements, so this is not a valid matrix:
B = [1 2 3 4 5 6; 1 2 3 4 5 6; 1 2 3 4 5; 1 2 3 4; 1 2 3]
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Would you like to replace the zeros with NaNs instead?
Robert Bridges
Robert Bridges 2021년 12월 7일
Yes, NaNs would work as long as afer this I can take the final real value from each row and input this into a new matix.
I've edited the question to include the final matrix B.

댓글을 달려면 로그인하십시오.


Jan
Jan 2021년 12월 7일
What is the wanted output?
A = [1 2 3 4 5 6; 1 2 3 4 5 6; 1 2 3 4 5 0; 1 2 3 4 0 0; 1 2 3 0 0 0];
B1 = A;
B1(B1 == 0) = NaN
B1 = 5×6
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 NaN 1 2 3 4 NaN NaN 1 2 3 NaN NaN NaN
B2 = A(A~=0)
B2 = 24×1
1 1 1 1 1 2 2 2 2 2
I'm not sure what this means: "pull out the end (non-zero) values from each row and put into a new n:1 matrix"
Maybe:
B = cell(height(A), 1);
for k = 1:height(A)
B{k} = A(k, A(k, :) ~= 0);
end
  댓글 수: 1
Robert Bridges
Robert Bridges 2021년 12월 7일
The final wanted output would be:
B = [6; 6; 5; 4; 3]
Using NaN's to get to this or other means isn't a problem
Edited in Question now.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by