replacing different elements in a matrix according to different conditions
Show older comments
Hi
I have an array which contains integers from 0 to 5. Now I want to replace all 5s with a 0, all 0 with a 5, the 2s with a 4 and vice versa and the 3 would not be changed. I know that I could do that with several nested if loops - but is there a faster/smarter way?
6 Comments
per isakson
on 11 May 2015
... and 1 would not change?
Joseph Cheng
on 11 May 2015
What do you do with 1's
MiauMiau
on 11 May 2015
Sebastian Castro
on 11 May 2015
Based on those rules, you could "cheat" and do this completely without logic:
out = 5 - in
MiauMiau
on 11 May 2015
Image Analyst
on 12 May 2015
There is a general function that lets you map any integer into any other number you want in a single line of code. It's general, not just "5-in" which coincidentally happens to work for this particular case. However based on your request - the only one I've ever gotten on over 15,000 questions answered - I won't answer.
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices 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!