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

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 Kommentar

Stephen23
Stephen23 am 8 Jul. 2016
Bearbeitet: Stephen23 am 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

Melden Sie sich an, um zu kommentieren.

Antworten (2)

José-Luis
José-Luis am 8 Jul. 2016

0 Stimmen

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 Kommentare

Stephen23
Stephen23 am 8 Jul. 2016
Bearbeitet: Stephen23 am 8 Jul. 2016
@José-Luis: all MATLAB arrays implicitly have infinite trailing singleton dimensions, and absolutely no errors are thrown when they are accessed:
>> x = [1,2;3,4];
>> x(2,2,1,1,1,1,1,1,1)
ans =
4
Perhaps it would be a good idea to do some research (read the documentation, or do an internet search), before writing incorrect answers on this forum.
José-Luis
José-Luis am 8 Jul. 2016
Bearbeitet: José-Luis am 8 Jul. 2016
That was not my point. I understand that. My point actually was that size() should return your actual size, not what you declared, or intended it to be. If the actual size was consistent with your declaration you would be getting errors if, e.g., a function expects vector and you actually passed a vector but declared it as a nd array. Good luck debugging that.
At least that's how I understood op 's question, he wants the size to be what he declares it to be not what it actually is.
And I do make errors but sometimes get it right. I could definitely do without the condescension.
Stephen23
Stephen23 am 8 Jul. 2016
Bearbeitet: Stephen23 am 8 Jul. 2016
@José-Luis: what exactly is the "actual size" of an array? Can you please show us where this is defined?
The MATLAB documentation for Multidimensional Arrays clearly states that "...every array technically has an infinite number of trailing singleton dimensions. A 3-by-2 array is the same as an array with size 3-by-2-by-1-by-1-by-1-by-..." and the size docs state that "Scalars are regarded as a 1-by-1 arrays". This would mean that any array's size "actually is" with trailing singletons.
You state in your answer that "Your array is two-dimensional", but the MATLAB docs state that every array really is infinite dimensional! It is only for human convenience (limited brain space) that we talk about the few lowest non-singleton dimensions, using simplification terms like "two dimensional". Just as all numbers have infinite digits, but we humans are most comfortable with the ones where almost all leading and trailing digits are zero.
You also stated that these are "non-existing dimensions", but the MATLAB docs clearly state that they (implicitly) exist. It is so much easier to ignore them, but does closing our eyes really make them go away?
" size() should return your actual size"
size does indeed give the actual size of any array:
>> x = 2;
>> [d1,d2,d3,d4,d5,d6] = size(x)
d1 =
1
d2 =
1
d3 =
1
d4 =
1
d5 =
1
d6 =
1
for as many trailing dimensions as you wish.
José-Luis
José-Luis am 8 Jul. 2016
Bearbeitet: José-Luis am 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 am 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!

Melden Sie sich an, um zu kommentieren.

Azzi Abdelmalek
Azzi Abdelmalek am 8 Jul. 2016

0 Stimmen

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

Kategorien

Tags

Gefragt:

am 8 Jul. 2016

Bearbeitet:

am 8 Jul. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by