How to interpret matrix indexation? Help to translate specific case please.

1 view (last 30 days)
Hi;
I am new using MATLAB, I regularly make programs in C++ and I am trying to translate the following piece of code into C++. I understand how repmat works and some of the indexations of matrices but I have been searching and I really have not found nor understood how does the last line works. I understand I am assigning "something" to F1, what I specifically do not understand is the part between brackets []. I reckon the colon is to "copy" the entire column and the number 1 in the ends refers that the F1 is the one I am getting the info. I my assumptions are correct please let me know.
I appreciate your time and your help to explain me. Thank you!
MATLAB code
nx=3; ny=3;
F=repmat(0.11,[nx ny 2]);
F(:,:,1)=F([nx 1:nx-1],:,1)
  1 Comment
dpb
dpb on 17 Mar 2014
Why don't you just look at it in the command window? Using some data other than a constant so you can see specifically who came from where would make it easier.
[nx 1:nx=1]
is a vector--in you specific case it's easily enough seen by simply plugging in values to be
[3 1:3-2] --> [3 1:2] --> [3 1 2]

Sign in to comment.

Accepted Answer

Marta Salas
Marta Salas on 17 Mar 2014
Edited: Marta Salas on 17 Mar 2014
You create a a 3dimesional matrix with the repmat: 3x3x2
The last line of code takes the matrix assigned on 3rd dimesion= 1 and reorders the rows. The code in brackets is the new order ([3 1 2] in your example)
  1 Comment
Rod
Rod on 2 Apr 2014
Thanks a lot Marta, I did not know it re-orders rows (or even columns) since I had a constant value in the matrix and it is not obvious visually. Now I understand ;)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!