how to fix this code ???
Show older comments
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
Answers (3)
Ced
on 16 Apr 2016
[ M_row, M_column ] = Untitled5();
Andrei Bobrov
on 17 Apr 2016
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
>>
Image Analyst
on 18 Apr 2016
0 votes
I thought I already showed you this before. This can be done simply by calling bwlabel() on each row, or column.
Categories
Find more on Geometric Transformation and Image Registration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!