replacing different elements in a matrix according to different conditions
Ältere Kommentare anzeigen
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 Kommentare
per isakson
am 11 Mai 2015
... and 1 would not change?
Joseph Cheng
am 11 Mai 2015
What do you do with 1's
MiauMiau
am 11 Mai 2015
Sebastian Castro
am 11 Mai 2015
Based on those rules, you could "cheat" and do this completely without logic:
out = 5 - in
MiauMiau
am 11 Mai 2015
Image Analyst
am 12 Mai 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.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!