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

3 次查看(过去 30 天)
pushkar
pushkar 2016-7-8
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 个评论
Stephen23
Stephen23 2016-7-8
编辑:Stephen23 2016-7-8
"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

请先登录,再进行评论。

回答(2 个)

José-Luis
José-Luis 2016-7-8
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 个评论
José-Luis
José-Luis 2016-7-8
编辑:José-Luis 2016-7-8
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 2016-7-8
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!

请先登录,再进行评论。


Azzi Abdelmalek
Azzi Abdelmalek 2016-7-8
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

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by