1

Taken I have some transition operations where certain numbers of my matrix (M) are being substituted with each other (all ones become 5, all 2s become 6, etc.). Now I know I can write a code like this:

if M(1,1)== 1 M(1,1)=5 end
if M (1,1)== 2 M(1,1)=6 end
if M(1,2)==1 M(1,2)=6 end

Of course if I have (10-by-10-by-10) matrix this is a lot of unnecessary work. Is there a possibility to either define the 3 dimensions (column, row, page) or to tell matlab something like:

% scan from point (1,1,1) to point (10,10,10) and apply mathematical operations when condition is fulfilled.

Thank you

kojikurac
  • 205
  • 2
  • 4
  • 11
  • 1
    the same solution from your last question should apply (logical indexing): http://stackoverflow.com/questions/6718543/simplifying-if-loops-with-multiple-conditions/6718649#6718649 . I suggest you read this indexing guide: http://www.mathworks.com/help/techdoc/math/f1-85462.html – Amro Jul 17 '11 at 17:21

2 Answers2

3

There is a simple command that changes all as to bs in an array using logical indexing. For example,

B = (M == 1);
M(B) = 5;

should change all 1s in M to 5s.

PengOne
  • 48,188
  • 17
  • 130
  • 149
  • yes, thank you as well - but the operations I'm using are not as simple as these in the example and thus it is necessary for me to be able to define (x,y,z) so I can say thing like: if M(x,y,z) < M(x+1,y,z) than this and this... Thomases answer helps me a lot already :) – kojikurac Jul 17 '11 at 17:04
  • @kojikurac: beware of the edges in such a case! For example when your condition is "if M(i,j,k) < M(i+1,j,k)", be careful when handling i = I (when you have an I*J*K matrix). In general, you will get better performance from this solution (if your operation is fast, if a single operation takes a lot of time, this solution might be slower) than from the other one. Also; somebody familiar with MATLAB will be able to understand your code faster (MATLAB people generally dislike `for` loops). – Egon Jul 17 '11 at 17:25
  • @kojikurac @egon: The main reason to avoid `for` loops in MATLAB is that they are incredibly slow. Matrix operations, on the other hand, are well optimized. Even if your test condition is similar to your description, there is probably a way to turn that into a matrix operation and you will get much better performance. – PengOne Jul 17 '11 at 18:18
  • thanks! You guys are all real geniuses here! Slowly I'm getting where I want to with my algorithmic composition and of course, I try to optimize my codes with each new idea, so I'm certain that at one point I'll have to take care of the good performance. So far, my codes are not that complicated and I can allow myself a _for_ loop or 2 :) – kojikurac Jul 17 '11 at 19:20
1

I'm not too familiar with MATLAB, but it seems like you should be able to define 3 scalar variables i, j, and k, and use 3 nested for loops to iterate over every ordered triple (x, y, z) with 0 <= x,y,z < 10... performing the switching logic inside the innermost loop.

Patrick87
  • 27,682
  • 3
  • 38
  • 73