Indexing (l,m.n) matrix for n=1,2,3,....?

2 visualizaciones (últimos 30 días)
pushkar
pushkar el 8 de Jul. de 2016
Editada: José-Luis el 8 de Jul. de 2016
x=zeros(2,2,1)
x =
0 0
0 0
>> size(x)
ans =
2 2
But it should be '2 2 1' Now if i consider x=zeros(2,2,2) then size(x)
ans =
2 2 2
but i need to access x(:,:,1) in the code which i am unable to do.
Pleas help
  1 comentario
Stephen23
Stephen23 el 8 de Jul. de 2016
Editada: Stephen23 el 8 de Jul. de 2016
"i need to access x(:,:,1) in the code which i am unable to do."
Actually it works perfectly, because every MATLAB array implicitly has infinite trailing singleton dimensions:
>> x = [1,2;3,4]
x =
1 2
3 4
>> size(x)
ans =
2 2
>> x(2,2,1,1,1,1,1,1,1)
ans =
4

Iniciar sesión para comentar.

Respuestas (2)

José-Luis
José-Luis el 8 de Jul. de 2016
Why would you wanna do that? Your array is two-dimensional, despite the way you defined it.
It would be a nightmare if you could append non-existing dimensions willy-nilly. You would be left scratching your head and/or throwing your monitor out of the window when at some point further down your program you get non-sensical error messages about array dimensions not matching when they in fact do.
  5 comentarios
José-Luis
José-Luis el 8 de Jul. de 2016
Editada: José-Luis el 8 de Jul. de 2016
Well, it seems this question proved to be a good workout. I get your point but, either you don't follow mine or are willfully misinterpreting my meaning.
op said:
But it should be 2 2 1
referring to the size of an array he instantiated as:
x=zeros(2,2,1)
and you are saying that it actually is, but we don't show it because of our limited brains.
Fair enough, point granted, you are totally right.
However, what I am saying is that you should not explicitly conserve however many singleton dimensions you want there to be.
In order to keep your code sane, singleton dimensions are effectively ignored (even if an infinity of them actually exist). What I am saying is that it would be a nightmare if you could explicitly conserve those dimensions when they are not required.
I am not talking about accessing values using however many indexes you want. I am talking about explicitly conserving singleton dimensions, which IMO, is a terrible idea, besides whatever rationalization TMW decided to put on the documentation for multidimensional arrays.
I was not answering the:
but i need to access x(:,:,1) in the code which i am unable to do
which is what your seem to have reacted to.
Stephen23
Stephen23 el 8 de Jul. de 2016
Aha... we are talking at cross-puposes :)
"it would be a nightmare if you could explicitly conserve those dimensions"
Agreed, this would likely by cause latent problems, that would be hard to track down.
Your comment above made your answer a lot clearer.... Thank you for the discussion!

Iniciar sesión para comentar.


Azzi Abdelmalek
Azzi Abdelmalek el 8 de Jul. de 2016
x=zeros(2)
the size of x is 2x2, but you can also consider it as 2x2x1x1x1x...x1. Now if you want to use x(:,:,1) there is no problem
x(:,:,1)
%or
x(:,:,1,1)
you will get the result

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by