Info
此问题已关闭。 请重新打开它进行编辑或回答。
extruct non zeros values and put into a short matrics
1 次查看(过去 30 天)
显示 更早的评论
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
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
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
2013-11-18
this is good...
out=cell2mat(arrayfun(@(x) A(x,A(x,:)~=0),(1:size(A,1))','un',0))
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!