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.

Réponses (4)

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 commentaires

Thankyou for the fast reply,
I have since specified b = [1,1] and c = [1,1],
but when running the code I still cant get the code to return b = 1.
I can see the value in the matrix as it comes up with a 1 in the element and zeros everywhere else (as it reduces through the matrix), but when it comes to outputting a value of 1 it still outputs -1.
I also had to specify the specific column as well as the (:) specifier for the columns only allows three iterations.
Thanks.
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?"
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?
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.

Connectez-vous pour commenter.

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 commentaires

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....
No I haven't figured out how to do it yet.
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

Connectez-vous pour commenter.

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
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

Catégories

Question posée :

Jay
le 5 Avr 2014

Réponse apportée :

Jay
le 6 Avr 2014

Community Treasure Hunt

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

Start Hunting!

Translated by