How to use if statements for a vector?
조회 수: 2 (최근 30일)
이전 댓글 표시
if choice==2
fprintf('Converting Resistance to Color Bands');
R = input('\n\nEnter the resistance in ohms as a vector: ');
% if R(3:end)~=0
% error('Invalid resistance entered. Program terminated.')
% end
ColorBand = Resist2Color(R);
fprintf('The color bands for that resistance are %s, %s, and %s',ColorBand(1),ColorBand(2),ColorBand(3));
end
Here's my code, the part im having trouble with is the commented out section. For example, if R = [1 2 0 0] my code runs fine because the numbers after the first two columns are 0. However, if R = [0 1 0 5] my code still exceutes since MATLAB only cares if any number in my specified range is 0 and disregards those>0. How can I fix this so when there's any number after the first two that doesn't equal 0 the error pops up.
댓글 수: 0
채택된 답변
Walter Roberson
2021년 9월 28일
if any(R(3:end)~=0)
It happens that you can abbreviate that to
if any(R(3:end))
댓글 수: 2
Walter Roberson
2021년 9월 28일
The code already posted will execute successfully as false if R is length 2.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!