The colon notation used in multidimensional array

I have come across the following notation in MATLAB
y(:, :, :, 2) = y(:, :, :, 1);
y(:, 1, :, 4) = y(:, 1, :, 3);
I have no clue what it means?
I know for matrix A(:,1) and A(1,:) means extract first column and first row respectively. But the above is not clear to me.
Thanks.

 Respuesta aceptada

Adam
Adam el 28 de Abr. de 2015

2 votos

It is just an extension of what you already know, but for multi-dimensional arrays. You can no longer use terms like 'rows' and 'columns' unless you have n ever more creative names for each dimension, but the logic is the same. Take all data from the dimensions that have a : and only the specified data from the dimensions which you give an exact index for.
Personally I rarely if ever use arrays of dimensionality higher than 3 because I like to be able to visualise what indexing means and since I can't visualise beyond a 3d cube I easily get confused. That said, the logic works the same whether it can be visualised intuitively or not!

4 comentarios

DM
DM el 28 de Abr. de 2015
wait so does A(:,1) mean take all rows and first column, or just first column? Because your statement Take all data from the dimensions that have a : and only the specified data from the dimensions which you give an exact index for. confused me.
Adam
Adam el 28 de Abr. de 2015
Editada: Adam el 28 de Abr. de 2015
A(:,1) says take all rows, 1st column.
My statement may not have been worded perfectly, but it should be interpreted as a 1 (or a 6 or 34 or whatever) as an index will take just the data for that specific index into that dimension. Any dimension where you specify a : takes all data from that dimension.
DM
DM el 28 de Abr. de 2015
Editada: DM el 28 de Abr. de 2015
But it gives me just second column not all rows. See example below
A=[2, 3, 4; 0, 5, 7]
A =
2 3 4
0 5 7
> A(:, 2)
ans =
3
5
Adam
Adam el 28 de Abr. de 2015
Editada: Adam el 28 de Abr. de 2015
It gives you all rows of the 2nd column. It's basically like a filter. It would give you all rows, but you told it to only give you column 2 so all other elements of the rows are thrown away.

Iniciar sesión para comentar.

Más respuestas (1)

Guillaume
Guillaume el 28 de Abr. de 2015
The colon is exactly the same as writing 1:end. In your example, naming the dimensions y, x, z, and t
y(:, :, :, 2) = y(:, :, :, 1);
copy all the values at t = 1 to t = 2 and
y(:, 1, :, 4) = y(:, 1, :, 3);
copy all the values at x = 1 and t = 3 to x = 1 and t = 4.

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

DM
el 28 de Abr. de 2015

Editada:

el 28 de Abr. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by