Skipping calculations using if
1 次查看(过去 30 天)
显示 更早的评论
Hello,
I have the following code:
for j = 1:(length(acalc)-1)
for ii = (j+1):length(acalc)
dotp(count2,:) = dot(acalc(j,:),acalc(ii,:));
magp(count2,:) = mag(j)*mag(ii);
count2 = count2 + 1;
end
end
Which works fine if length(acalc) > 1. However when length(acalc) = 1 (as it sometimes is), an error is returned presumably because of the failure in accessing acalc(ii,:) = acalc(j+1,:) = acalc(2,:)
Could someone help me resolve this issue? i.e. by simply skipping the calculations if length(acalc) = 1?
I tried simply stating if length(acalc) > 1 before this section of code, but that didn't seem to work.
Many thanks,
Tom
0 个评论
回答(1 个)
Matt J
2013-12-3
No, if length(acalc)=1, the code you've shown would never be executed. For example, running the following will not result in anything displayed.
acalc=10;
for j = 1:(length(acalc)-1)
disp 'Executing'
end
4 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!