필터 지우기
필터 지우기

matrix problem not getting desired result.

조회 수: 1 (최근 30일)
vaishali
vaishali 2013년 9월 10일
A=[0 0 0 0 0;0 1 1 0 0;0 1 0 0 0;0 1 1 0 0]; [rows, cols] = find(A == 1); n1=min(cols); n2=max(cols); m1=min(rows); m2=max(rows); for i=n1:n2 count=0; for j=m1:m2 if A(j,i)==1 count=count+1; end end count1=count; end
RESULT: count1=2
REQUIRED RESULT: count1=[3 2]

채택된 답변

Geert
Geert 2013년 9월 10일
편집: Geert 2013년 9월 10일
Please try to format you question properly next time.
You want to know how many non-zero entries there are in each column I guess?
The easiest way of doing so is like this:
A=[0 0 0 0 0;0 1 1 0 0;0 1 0 0 0;0 1 1 0 0];
countPerColumn = sum(A==1,1);
% if you want to remove the zero entries you can add the following line:
countPerColumnRemovedZeros = countPerColumn;
countPerColumnRemovedZeros(countPerColumn == 0) = [];
If you want to adjust your own code (which is a bit sloppy in my opinion), you probably want something like this:
A=[0 0 0 0 0;0 1 1 0 0;0 1 0 0 0;0 1 1 0 0];
[rows, cols] = find(A == 1);
n1=min(cols); n2=max(cols);
m1=min(rows); m2=max(rows);
columns = n1:n2 ;
for ii=1:length(columns);
count=0;
for j=m1:m2
if A(j,columns(ii))==1
count=count+1;
end
end
count1(ii)=count;
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by