how to correct this error ?

조회 수: 8 (최근 30일)
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016년 4월 5일
댓글: Image Analyst 2016년 4월 5일
% M_row to return the number of ones in each row
% M_column to return the number of ones in each column
M =[ 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1
1 0 0 1 0 0 1 1 1 1 1 0 1 0 1
1 1 1 0 1 1 1 1 1 1 0 0 1 0 0
1 0 0 1 0 0 1 1 1 1 1 1 0 1 1
1 1 0 1 0 1 0 1 1 1 0 0 1 0 0
1 1 1 1 1 1 0 1 1 0 1 0 1 0 1
1 1 1 1 1 1 0 1 0 1 1 1 1 1 1
1 1 1 1 1 0 0 0 0 0 1 1 1 1 1
1 1 1 1 1 0 0 0 1 0 1 1 1 1 1
1 1 1 1 1 0 0 0 1 0 1 1 1 1 1
1 1 1 1 0 0 0 0 0 1 0 1 1 1 1
1 1 1 0 0 0 0 0 0 0 1 0 1 1 1
1 1 1 0 0 0 0 0 0 0 1 0 1 1 1
1 1 1 1 0 0 0 0 0 1 0 1 1 1 1
1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 ];
[n_M,m_M]=size(M);
b_M=cell(n_M,1);
c_M=cell(1,m_M);
maxb=1;
maxc=1;
for k=1:n_M
a=[0 M(k,:) 0];
ii1=strfind(a,[0 1]);
ii2=strfind(a,[1 0]);
maxb=max(maxb,numel(ii1));
b{k}=ii2-ii1;
end
for k=1:m_M
a=[0 M(:,k)' 0];
ii1=strfind(a,[0 1]);
ii2=strfind(a,[1 0]);
maxc=max(maxc,numel(ii1));
c_M{k}=(ii2-ii1)';
end
M_row=cell2mat(cellfun(@(x) [x zeros(1,maxb-numel(x))],b_M,'un',0));
M_column=cell2mat(cellfun(@(x)[zeros(maxc-numel(x),1);x],c,'un',0));
%%find the sum numbers in each column and count them
% x to return the sum of the number
% w to return number of element in each column
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Col=(n_M+repmat(1:n_M,m_M,1)-A'.*cumsum(A',2)).*A';
for k=1:m_M
a=Col(k,Col(k,:)~=0);
[~,~,kk]=unique(a);
col1{k,1}=accumarray(kk,1);
end
celldisp(col1)

채택된 답변

Image Analyst
Image Analyst 2016년 4월 5일
편집: Image Analyst 2016년 4월 5일
Wow, you sure do know how to complicate things. If you want to have M_row be "the number of ones in each row, and M_column be the number of ones in each column", you can simply do
M_row = sum(M, 2);
M_column = sum(M);
Instead of what you have:
M_row=cell2mat(cellfun(@(x) [x zeros(1,maxb-numel(x))],b_M,'un',0));
M_column=cell2mat(cellfun(@(x)[zeros(maxc-numel(x),1);x],c,'un',0));
Anyway, do you have an error that you want to ask about, but forgot to share with us?
  댓글 수: 2
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016년 4월 5일
No No M_row does not mean the number of ones in each row
M_row mean that group of ones in each row like this
the first row is [1 1 0 1 0 1 1 1 1 1 1 1 1 1 1] which mean [2 1 10]
Image Analyst
Image Analyst 2016년 4월 5일
OK, I'll tell you how, but I still want to know WHY. Perhaps it would be easier if you just named your variables appropriately, like numberOfRegionsPerRow rather than M_row
[rows, columns] = size(M);
M_row = zeros(rows, ceil(columns/2));
for row = 1 : size(M, 1)
% Extract just this row.
thisRow = M(row, :);
% Measure the lengths of all "runs" of 1s.
measurements = regionprops(logical(thisRow), 'Area');
% Extract all the lengths into one vector.
allLengths = [measurements.Area];
% Assign them to the left-most part of the M-Row row.
M_row(row, 1:length(allLengths)) = allLengths;
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by