I am trying to figure out why my for and if statement are not returning a value to be looked up in a matrix
이전 댓글 표시
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.
답변 (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
Jay
2014년 4월 5일
Image Analyst
2014년 4월 5일
Justin, you didn't answer my question. Here is a:
a =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
Now, what do you want b and c to be after you run your loop? Inside the loop, you are just repeatedly overwriting them with a scalar, so b and c are not arrays in the loop. If you want them to be arrays you must give them indexes, like i or row and column. Do you want them to be 1 by 2 arrays, a scalar, or a 3 by 5 array? So please please answer my question from before "Please give exactly what forms you expect arrays b and c to take. What should they look like for your example a?"
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.
댓글 수: 3
Image Analyst
2014년 4월 6일
Uh, okay. So you did it. d never changes and the logical comparison of d==[] can be done by isempty(d) but you never check the result anyway, and b and c get overwritten with a scalar though they started as a 1 by 2 array, so all that's kind of weird, but whatever....
Jay
2014년 4월 6일
Jay
2014년 4월 6일
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!