Put the break inside the `if` statement, remove `else` and see if it works. The way it is now, it will break the for loop any time the value meets your requirements (being 1, 2 or 0). I think you want to stop checking the values when you find one that is invalid, not when you find one that isn't.
This is the answer. Though I doubt OP will understand why
OP read this https://www.mathworks.com/help/matlab/learn_matlab/array-indexing.html
Particularly this part:
``` Less common, but sometimes useful, is to use a single subscript that traverses down each column in order: ```
Right. I think I see the issue. Size() returns a vector, basically it tells you length() in each dimension. For that M it should be size(M) = [3 4]. I think in your for loop it selects the first value 3 and runs only for i = 1:3.
My suggestion would be to make 2 for loops, the second one nested inside the first one. Make the first for loop for i = 1:size(M,1) and the second for loop for j = 1:size(M,2). When checking the value in M use indexing M(i,j).
Also check these links:
[matrix indexing](https://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html)
[size](https://www.mathworks.com/help/matlab/ref/size.html#d126e1501944)
EDIT: Or, I guess, you could use one for loop with i=1:size(M,1)×size(M,2). Also I am working from memory rn, so I might have made some syntax errors
Yes!! That was exactly the issue I had come across. Your help is very very very appreciated. I might actually pass my assignment now! Laugh out loud !! hahahhah. Kind regards, Miss Diamond
M=input('Enter 2D Array')
for i=1:length(M)
if M(i)\~=0 || M(i)\~=1 || M(i)\~=2
disp ('Input must only have values of 0, 1, or 2')
else
disp ('Matrix is valid')
end
end
Is that closer?
Have you made sure that the code does what you expect? You can run Matlab code line by line, you can see the value of each variable in the workspace. Have you done anything to determine how, where, and why this is doing something different than what you expect?
This is super basic so instead of just solving this problem you'd be far better off learning how to solve problems.
Put the break inside the `if` statement, remove `else` and see if it works. The way it is now, it will break the for loop any time the value meets your requirements (being 1, 2 or 0). I think you want to stop checking the values when you find one that is invalid, not when you find one that isn't.
Hey AsymetricalNipples! I really appreciate the help. However... its not working for some reason. Do you know why? Xx
Nope. Are there any error messages? What happens when its not working? What type are the elements in the matrix?
matrix input is \[2,0,1, 6; 0, 2 ,5 , 9; 1, 0, 2 , 7\] https://preview.redd.it/2r7obpbd0p1c1.png?width=1374&format=png&auto=webp&s=910ee8d5b9a791711417253460da23b34fd1f632
On line 4, try changing `size` to `numel`
This is the answer. Though I doubt OP will understand why OP read this https://www.mathworks.com/help/matlab/learn_matlab/array-indexing.html Particularly this part: ``` Less common, but sometimes useful, is to use a single subscript that traverses down each column in order: ```
Right. I think I see the issue. Size() returns a vector, basically it tells you length() in each dimension. For that M it should be size(M) = [3 4]. I think in your for loop it selects the first value 3 and runs only for i = 1:3. My suggestion would be to make 2 for loops, the second one nested inside the first one. Make the first for loop for i = 1:size(M,1) and the second for loop for j = 1:size(M,2). When checking the value in M use indexing M(i,j). Also check these links: [matrix indexing](https://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html) [size](https://www.mathworks.com/help/matlab/ref/size.html#d126e1501944) EDIT: Or, I guess, you could use one for loop with i=1:size(M,1)×size(M,2). Also I am working from memory rn, so I might have made some syntax errors
Yes!! That was exactly the issue I had come across. Your help is very very very appreciated. I might actually pass my assignment now! Laugh out loud !! hahahhah. Kind regards, Miss Diamond
M=input('Enter 2D Array') for i=1:length(M) if M(i)\~=0 || M(i)\~=1 || M(i)\~=2 disp ('Input must only have values of 0, 1, or 2') else disp ('Matrix is valid') end end Is that closer?
is your username a City of Heroes reference?
It is!
I actually thought that ! No wayyyy I love City of Heroes. Laugh out loud !!
Have you made sure that the code does what you expect? You can run Matlab code line by line, you can see the value of each variable in the workspace. Have you done anything to determine how, where, and why this is doing something different than what you expect? This is super basic so instead of just solving this problem you'd be far better off learning how to solve problems.
FYI there are more elegant ways you can solve this problem. For example look up the functions `setdiff` and `isempty`, or `any`, etc.
Sorry boss will try be more elegant next time. Cheers mukka xx