Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How does (:) operator work???

Subject: How does (:) operator work???

From: Nikolas tuc

Date: 5 Feb, 2010 06:00:22

Message: 1 of 5

Hi there,

I have a 3d matrix R(5,5,3) and I am doing the following operations:
for i = 1:5
   for j =1:5
       rt = R(i, j, :);
       rt = rt(:);
    end
end

What does the rt = rt(:) mean? What is the new form that the rt matrix gets?
I would really appreciate if you could send me a numerical example.

Thank you in advance.
NM

Subject: How does (:) operator work???

From: Matt Fig

Date: 5 Feb, 2010 06:14:05

Message: 2 of 5

Try and figure it out!

A = magic(3)
A(:)

A = randperm(6)
A(:)

Subject: How does (:) operator work???

From: us

Date: 5 Feb, 2010 06:17:04

Message: 3 of 5

"Nikolas tuc" <nmihailidis@gmail.com> wrote in message <hkgc5l$9uo$1@fred.mathworks.com>...
> Hi there,
> I have a 3d matrix R(5,5,3) and I am doing the following operations:
> for i = 1:5
> for j =1:5
> rt = R(i, j, :);
> rt = rt(:);
> end
> end
> What does the rt = rt(:) mean? What is the new form that the rt matrix gets?
> I would really appreciate if you could send me a numerical example.
>
> Thank you in advance.
> NM

a hint:

     help colon;
% then, get your hands dirty...
     v=1:4
     v=v(:)
% ...and simply play with ML...

us

Subject: How does (:) operator work???

From: Jeff

Date: 5 Feb, 2010 07:12:02

Message: 4 of 5

"Nikolas tuc" <nmihailidis@gmail.com> wrote in message <hkgc5l$9uo$1@fred.mathworks.com>...
> Hi there,
>
> I have a 3d matrix R(5,5,3) and I am doing the following operations:
> for i = 1:5
> for j =1:5
> rt = R(i, j, :);
> rt = rt(:);
> end
> end
>
> What does the rt = rt(:) mean? What is the new form that the rt matrix gets?
> I would really appreciate if you could send me a numerical example.
>
> Thank you in advance.
> NM

Using the variable editor, it seems that rt=R(i,j,:) keeps rt in 3-dimensions.

I created R(5,5,3). R(5,5,1)=11, R(5,5,2)=12, R(5,5,3)=13.

Then, rt = R(i, j, :); created rt as a 1x1x3 with the following:

val(:,:,1) = 11
val(:,:,2) = 12
val(:,:,3) = 13

Finally, rt=rt(:) turns rt into a regular ol' vector with values [11;12;13].

So it seems that the first line, rt = R(i, j, :), is retaining all the dimensions, even when they are only one. Whereas rt(:) is enumerating all the values and storing them in rt. So this last line just gets rid of any 1-long dimensions is my guess.

I'm new to this wonderful Matlab array referencing, too. It's fun, eh? You may get a few tips if you can find some of my recent posts where I asked similar questions.

Subject: How does (:) operator work???

From: Parker

Date: 5 Feb, 2010 09:31:20

Message: 5 of 5

On Feb 5, 6:00 am, "Nikolas tuc" <nmihaili...@gmail.com> wrote:
> Hi there,
>
> I have a 3d matrix R(5,5,3) and I am doing the following operations:
> for i = 1:5
>    for j =1:5
>        rt = R(i, j, :);
>        rt = rt(:);
>     end
> end
>
> What does the rt = rt(:) mean? What is the new form that the rt matrix gets?
> I would really appreciate if you could send me a numerical example.
>
> Thank you in advance.
> NM

==========
rt = R(5,5,1:3)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us