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!