I am trying to figure out why my for and if statement are not returning a value to be looked up in a matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
Can somebody please tell me why my following code isn't working please?
a = [1,2,3,4,5;6,7,8,9,10;11,12,13,14,15]
a = a'
for i = 1:15
if a(i:5,:) == 3
b = 1
else
c = -1
end end
Where am I going wrong to find the value 3?
Thanks.
댓글 수: 0
답변 (4개)
Image Analyst
2014년 4월 5일
편집: Image Analyst
2014년 4월 5일
Your if statement takes an array, not a boolean, and the results are unpredictable (at least by you). Here, try it this way and look at what value myCondition spits out to the command window. It will be instructive for you:
a = [1,2,3,4,5;6,7,8,9,10;11,12,13,14,15]
a = a'
for i = 1:15
myCondition = a(i:5,:) == 3
if myCondition
b = 1
else
c = -1
end
end
Please give exactly what forms you expect arrays b and c to take. What should they look like for your example a?
댓글 수: 4
Image Analyst
2014년 4월 5일
Justin's two "Answers" moved here since they are not answers to his original question but are comments to me.
I need the values to be a Boolean variable.
I say this because I wish to apply that specific value to look up that Boolean variable from a much larger array with correlating values to be used for multiple equations.
Is this possible?
As for your question as to what I want b and c to do, I want them to hold a variable integer value to be used to look up those integer values and return the correlating Boolean cell value to added onto a cell Boolean value from another array.
Sorry I just looked up booleans meaning.
I thought it was a true value rather than a yes / no => apply function type.
Is their a way to have a variable value rather than a Boolean?
Image Analyst
2014년 4월 5일
Justin, after 4 statements from you, I still don't know what you want. Please write down the output array. Here I go guessing again....
Maybe you want this:
b = find(a == 3);
c = find(a ~= 3);
You might not even need the find depending on what "to added onto a cell Boolean value from another array" means. You may be able to just use b=(a==3) to get a logical "map" of there the 3's are.
Or, maybe you want to use the ismember() function.
참고 항목
카테고리
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!