필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

extruct non zeros values and put into a short matrics

조회 수: 1 (최근 30일)
Asl
Asl 2013년 11월 18일
마감: MATLAB Answer Bot 2021년 8월 20일
A =
0 0 0 0 5 0 0 8 0 0 0 0 8
0 0 0 5 0 0 0 8 0 0 0 0 9
0 0 0 5 0 0 0 7 0 0 0 0 8
0 0 0 0 5 0 0 0 8 0 0 0 9
0 0 0 6 0 0 0 8 0 0 0 0 9
0 0 0 5 0 0 0 7 0 0 0 0 8
0 0 0 8 0 0 0 8 0 0 0 0 9
I want to extruch a small matrics (with 3 columns) containing only non zeros values
Results:
5 8 8
5 8 9
5 7 8
5 8 9
6 8 9
5 7 8
8 8 9
ho I can do this smartly?
thanks
  댓글 수: 1
Image Analyst
Image Analyst 2013년 11월 18일
To do it smartly and robustly, you'd check to make sure that there are the same number of zeros in each row. If there aren't you'd need to decide how to handle that, for example, take the first or last three
threeValues = find(oneRow~= 0, 3, 'first'); % Could be empty, 1, 2, or 3 at most.
if length(threeValues) == 3 .... etc.

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 18일
편집: Azzi Abdelmalek 2013년 11월 18일
out=cell2mat(arrayfun(@(x) A(x,A(x,:)~=0),(1:size(A,1))','un',0))
%or
n=size(A,1);
B=zeros(n,3);
for k=1:n
B(k,:)=A(k,A(k,:)~=0);
end
B
  댓글 수: 2
The Matlab Spot
The Matlab Spot 2013년 11월 18일
this is good...
out=cell2mat(arrayfun(@(x) A(x,A(x,:)~=0),(1:size(A,1))','un',0))
Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 18일
Not necessary, the second using for loop is slightly faster then arrayfun

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by