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

5 views (last 30 days)
pushkar
pushkar on 8 Jul 2016
Edited: José-Luis on 8 Jul 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 Comment
Stephen23
Stephen23 on 8 Jul 2016
Edited: Stephen23 on 8 Jul 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

Sign in to comment.

Answers (2)

José-Luis
José-Luis on 8 Jul 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 Comments
José-Luis
José-Luis on 8 Jul 2016
Edited: José-Luis on 8 Jul 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 on 8 Jul 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!

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 8 Jul 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

Tags

Community Treasure Hunt

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

Start Hunting!