I have tried using mod function but i get error of logical expression in command window. I am new to this so little help will be appreciated. I can do this with with arrays but with matrix i am getting error. Please tell me the correct approach.
Asked
Active
Viewed 35 times
-6
-
2What's your error? ***EDIT*** your question to add (a) your code using `mod`, (b) the full error message and (c) the line at which the error occurs. Always include these three things in your questions – Dan May 28 '15 at 10:42
1 Answers
2
If A
is your matrix and n
is your scalar:
A = magic(5);
n=2;
Then to manipulate (e.g. multiply by 1000
) the elements of A
that are divisible by n
, just do:
idx = mod(A,n)==0;
A(idx) = A(idx)*1000;

Dan
- 45,079
- 17
- 88
- 157
-
2I don't know why there is a down-vote, but this solution is clear and good for what the question is. – Santhan Salai May 28 '15 at 11:13
-
1@SanthanSalai - I get random downvotes when I answer in a clear manner such as this example by Dan (+1 btw). They're mostly done by people who don't answer questions in the MATLAB tag and are those who find these questions in the first-time user review queues. They're under the mentality that if the OP has shown no effort, then we shouldn't answer the questions and the -1 is to discourage answering. Me personally I don't care. If I think the question is interesting, regardless of the effort, I will answer it as we want to share knowledge. I just don't let them bother me and move on. – rayryeng May 28 '15 at 15:45
-
1@SanthanSalai - Besides which, I believe that I'll receive more upvotes than down and so they'll cancel out in the end :) – rayryeng May 28 '15 at 15:45
-
1@rayryeng Completely agree. BTW this happened for me today also for [this answer](http://stackoverflow.com/questions/30471358/finding-a-critical-point-in-matrix/30471663#30471663) somebody came, downvoted and went silently. – Santhan Salai May 28 '15 at 15:51
-
-
@manroop your question was unclear and didn't include the three things I mentioned in my comment which you had ample time to add : your choice from your attempts, complete error message, line at which error occurred – Dan May 28 '15 at 19:09