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

9 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
Stephen
Stephen 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
Stephen
Stephen 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!