Info

この質問は閉じられています。 編集または回答するには再度開いてください。

how to alter this code because its give me error/

1 回表示 (過去 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