how to fix this code ???
조회 수: 2 (최근 30일)
이전 댓글 표시
when i run this code the function return just M_row
function [M_row , M_column] = Untitled5 (~)
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]=size(M);
b=cell(n,1);
c=cell(1,m);
maxb=1;
maxc=1;
for k=1:n
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
a=[0 M(:,k)' 0];
ii1=strfind(a,[0 1]);
ii2=strfind(a,[1 0]);
maxc=max(maxc,numel(ii1));
c{k}=(ii2-ii1)';
end
M_row=cell2mat(cellfun(@(x) [x zeros(1,maxb-numel(x))],b,'un',0));
M_column=cell2mat(cellfun(@(x)[zeros(maxc-numel(x),1);x],c,'un',0));
end
I need when i run this code . i need to return M_row and M_column
댓글 수: 0
답변 (3개)
Andrei Bobrov
2016년 4월 17일
function [M_row , M_column] = mainfun(M)
M_row = onestep(M.',1)';
M_column = onestep(M,2);
function out = onestep(M,stl)
v = [M(1,:); diff(M); -M(end,:)];
[i1,j1] = find(v == 1);
[i2,~] = find(v == -1);
i1 = i2 - i1;
t = max(diff([find([1;diff(j1)]);numel(i1)+1]));
if stl == 1
f = @(x){[x(:); zeros(t-numel(x),1)]};
else
f = @(x){[zeros(t-numel(x),1); x(:)]};
end
c = accumarray(j1,i1,[],f);
out = [c{:}];
Use function mainfun
>> M
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
>> [M_row , M_column] = mainfun(M)
M_row =
2 1 10 0 0
1 1 5 1 1
3 6 1 0 0
1 1 6 2 0
2 1 1 3 1
6 2 1 1 1
6 1 6 0 0
5 5 0 0 0
5 1 5 0 0
5 1 5 0 0
4 1 4 0 0
3 1 3 0 0
3 1 3 0 0
4 1 4 0 0
5 5 0 0 0
M_column =
0 0 0 0 0 0 0 0 0 0 2 0 0 0 0
0 0 0 0 0 0 0 0 0 5 1 1 0 0 0
0 1 0 2 1 1 0 0 0 1 5 1 0 1 2
0 1 1 8 5 1 0 0 6 1 2 5 3 1 1
15 11 10 2 1 3 4 7 2 1 1 2 11 9 10
>>
댓글 수: 0
Image Analyst
2016년 4월 18일
I thought I already showed you this before. This can be done simply by calling bwlabel() on each row, or column.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Elementary Polygons에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!