Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

how to alter this code because its give me error/

조회 수: 2 (최근 30일)
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016년 5월 2일
마감: MATLAB Answer Bot 2021년 8월 20일
M =[ 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1 0 0 0 0 0
0 0 0 0 1 1 1 1 1 1 1 0 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 0 0 0
0 0 1 1 1 1 1 1 1 1 1 1 1 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 0 1 0 1 1 1 1 1 1
0 1 1 1 1 0 0 1 0 0 1 1 1 1 0
0 0 0 0 0 0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 ];
[n,m]=size(M);
b=cell(n,1);
c=cell(1,m);
maxb=ceil(m/2);
maxc=ceil(n/2);
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));
M_C = (M_column)';
F=zeros(n,m); % preallocate output
ix=find(sum(M_row,2)+sum(M_row~=0,2)-1==m); % rows contain some ones
F(ix,:)=1; % start with all '1'
F(sub2ind(size(F),ix,M_row(ix,1)+1))=0 ;
  • give me error in F(sub2ind(size(F),ix,M_row(ix,1)+1))=0 ;
(Error using sub2ind Out of range subscript.)
  댓글 수: 4
Image Analyst
Image Analyst 2016년 5월 2일
Tell us what you want to do to that matrix. It seems unnecessarily complicated. I don't think you should have to converting to cell arrays and messing with cellfun() and cell2mat(), etc. It might be able to be done much simpler just as simple numerical matrices or images. Maybe even one line of code if you want a typical image processing type of operation. Just describe in words what you want to do. At least throw in some comments - the most glaring sign of bad code is no comments. But an overall description would be better than comments step-by-step in code that is taking a bad approach.
Stephen23
Stephen23 2016년 5월 2일
@Image Anaalyst: there have been ten or twenty questions (I didn't keep count) on this matrix, and similar variations. The requirements change each day, and so do the answers...
Task solving by iterative online-forum code generation.
@Firas Al-Kharabsheh: It would be useful to actually get a real explanation of what the goal is, and then perhaps we could suggest a robust way to approach the task.

답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by