Clear Filters
Clear Filters

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 view (last 30 days)
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.

Answers (4)

Image Analyst
Image Analyst on 5 Apr 2014
Edited: Image Analyst on 5 Apr 2014
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 Comments
Image Analyst
Image Analyst on 5 Apr 2014
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
Image Analyst on 5 Apr 2014
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.

Sign in to comment.


Jay
Jay on 6 Apr 2014
Edited: Jay on 6 Apr 2014
Here is what I wish to do in its entirety,
b = mat_res(:,1)
c = mat_res(:,2)
d =[]
e = [b,c]
mat_res = [1,2,3,4,5,6, 7,8,9,10,11,12,13,14,15; 22.132, 134.342, 59.984, 65.069, 59.001, 45.005,20.567, 34.609, 0.234, 685.356, 7.045, 2.908, 34.059, 67.905, 84.854]'
for i = 1:15
if b == 3 % look in mat_res(:,1) for int 3
e % populate e (:,1) with int value from mat_res, populate e(:,2)
% with matching col 2 (mat_res) value.
else
d % leave e (:,:) empty and move to next iteration.
end
end
I assume " : " means sequential value found in the cells and not always cell step values (1,2,3), so if I had a matrix with column 1 values 3,5,4,7 it would see that in cell 1 the value is 3, cell 2 would have a value of 5, cell 3 would have a value of 4 etc.
So using the previous paragraph values, the above example it would not look at cell 1 and say cell 1 == 1, cell 2 == 2 , cell 3 == 3 but rather cell 1 = 3 (has a value in the cell of 3), cell 2 =5 (has a value in the cell of 5), cell 3 = 4 (has a value in the cell of 4) etc.
I want matrix e to be populated in col 1 with all values = 3, I want matrix e col 2 to be populated with the matching value of 3 (59.984).
does this help explain exactly what I want to do?
Thanks for your patience.
  3 Comments
Jay
Jay on 6 Apr 2014
I have also tried this to no avail.
mat_res = [1,2,3,4,5,6, 7,8,9,10,11,12,13,14,15; 22.132, 134.342, 59.984, 65.069, 59.001, 45.005,20.567, 34.609, 0.234, 685.356, 7.045, 2.908, 34.059, 67.905, 84.854]'
b = mat_res(:,1)
c = mat_res(:,2)
e = [b,c]
for i = 1:15 if b(:,1) == 3 e(i,1) = b(i,1) e(i,2) = c(i,1)
else
e(i,1) = []
e(i,2) = []
end
end

Sign in to comment.


Jay
Jay on 6 Apr 2014
Edited: Jay on 6 Apr 2014
I have it running through and reducing so I feel I am almost there but having further issues specifying what values to remove and what values to keep?
mat_res = [1,2,3,4,5,6, 7,8,9,10,11,12,13,14,15; 22.132, 134.342, 59.984, 65.069, 59.001, 45.005,20.567, 34.609, 0.234, 685.356, 7.045, 2.908, 34.059, 67.905, 84.854]' b = mat_res(:,1)
c = mat_res(:,2)
d =[]
e = [b,c]
fix_val = [2,4,6]' sz_fix_val = size (fix_val) % specifies number of iterations to be used to compare fix_val
% create a for statement running through the fix_vals. create single array % that holds each val for so many iterations.
for i = 1:size (mat_res) for j = fix_val(1:size(sz_fix_val),1) % j specifies the values % need to specify the cell values. % specifies 3 times but 1:3 for k = (fix_val) if k(j,1) == mat_res(i,1) e(i,1) = e(i,1) e(i,2) = mat_res(i,2)
else
e(i,1) = 0
e(i,2) = 0
end
end
end
end

Jay
Jay on 6 Apr 2014
How do I get the loop to re-iterate until a condition is met?
I can not get the for loop statement to reiterate counting down 1 increment after all 15 values have been searched through.
I could really do with some more input.
mat_res = [1,2,3,4,5,6, 7,8,9,10,11,12,13,14,15; 22.132, 134.342, 59.984, 65.069, 59.001, 45.005,20.567, 34.609, 0.234, 685.356, 7.045, 2.908, 34.059, 67.905, 84.854]' b = mat_res(:,1)
c = mat_res(:,2)
d =[]
e = [b,c]
fix_val = [2,4,6]' sz_fix_val = size (fix_val) % specifies number of iterations to be used to compare fix_val k = 1 k_z = k v = fix_val(1,1) % s = 1 % p = fix_val (1+s,1) t = 1:size(sz_fix_val) [m_sz_fix_val,n_sz_fix_val]= size(fix_val) loop_red = [m_sz_fix_val,2] %loop_1 = 1:size(sz_fix_val) % create a for statement running through the fix_vals. create single array % that holds each val for so many iterations.
for q = 1 :m_sz_fix_val ~= 0 % once the below has gone through its increments, come back here reduce by one and start again.
for i = 1:size (mat_res)
v = v(t,1) % Create a step ! need to specify dynaic cell that checks the fix val elements
for j = fix_val(1:size(sz_fix_val),1) % j specifies the values % need to specify the cell values. % specifies 3 times but 1:3
v = v(t,1) % Create a step ! need to specify dynaic cell that checks the fix val elements
k=j
if k == mat_res(i,1)
e(i,1) = e(i,1)
e(i,2) = mat_res(i,2)
else
e(i,1) = 0
e(i,2) = 0
end
end
end
end

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!