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!