Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Removing array dimension

Subject: Removing array dimension

From: Jonathan W Smith

Date: 30 Apr, 2013 22:28:14

Message: 1 of 9

Hello
If I have an array of size 3 x 4 x 5 (3-dimensional array) .

How do completely remove the 3rd dimension so it will become a size of just 3 x 4 (2-dimensional array).

Lets say the array is A, I know I could do B = A(:,:,[]). However, in this case I would end up with a 3-dimensional array with the 3rd dimension being all zeros.

Thanks
Jonathan

Subject: Removing array dimension

From: James Tursa

Date: 30 Apr, 2013 23:19:09

Message: 2 of 9

"Jonathan W Smith" wrote in message <klpght$jh1$1@newscl01ah.mathworks.com>...
> Hello
> If I have an array of size 3 x 4 x 5 (3-dimensional array) .
>
> How do completely remove the 3rd dimension so it will become a size of just 3 x 4 (2-dimensional array).
>
> Lets say the array is A, I know I could do B = A(:,:,[]). However, in this case I would end up with a 3-dimensional array with the 3rd dimension being all zeros.

It is not clear what you want. If you want a specific page, e.g.,

B = A(:,:,1); % the 1st page

If you want a sum, e.g.,

B = sum(A,3); % sums along the 3rd dimension

Maybe you could give a short example of input and desired output.

James Tursa

Subject: Removing array dimension

From: dpb

Date: 30 Apr, 2013 23:54:00

Message: 3 of 9

On 4/30/2013 5:28 PM, Jonathan W Smith wrote:
> Hello
> If I have an array of size 3 x 4 x 5 (3-dimensional array) .
>
> How do completely remove the 3rd dimension so it will become a size of
> just 3 x 4 (2-dimensional array).
>
> Lets say the array is A, I know I could do B = A(:,:,[]). However, in
> this case I would end up with a 3-dimensional array with the 3rd
> dimension being all zeros.

Well, what do you want to happen to which planes of the 5? IOW, what is
the content of the resultant array just the content of the first plane?

If so, simply

A(:,:,2:5)=[];

Salt to suit or provide more reqm'ts if there are any...

--

Subject: Removing array dimension

From: Jonathan W Smith

Date: 1 May, 2013 01:26:11

Message: 4 of 9

dpb <none@non.net> wrote in message <klplip$grr$1@speranza.aioe.org>...
> On 4/30/2013 5:28 PM, Jonathan W Smith wrote:
> > Hello
> > If I have an array of size 3 x 4 x 5 (3-dimensional array) .
> >
> > How do completely remove the 3rd dimension so it will become a size of
> > just 3 x 4 (2-dimensional array).
> >
> > Lets say the array is A, I know I could do B = A(:,:,[]). However, in
> > this case I would end up with a 3-dimensional array with the 3rd
> > dimension being all zeros.
>
> Well, what do you want to happen to which planes of the 5? IOW, what is
> the content of the resultant array just the content of the first plane?
>
> If so, simply
>
> A(:,:,2:5)=[];
>
> Salt to suit or provide more reqm'ts if there are any...
>
> --
Lets say A is an array with dimensions 3 x 3 x 4.
A(:,:,1) = 1 2 3 A(:,:,2) = 10 11 12 A(:,:,3) = 18 20 21 A(:,:,4) = 28 29 30
               4 5 6 13 14 15 22 23 24 31 32 33
               7 8 9 16 17 18 25 26 27 34 35 36

When I input A(:,:,4) = [] I get the following

A(:,:,1) = 1 2 3 A(:,:,2) = 10 11 12 A(:,:,3) = 18 20 21 A(:,:,4) = 0 0 0
               4 5 6 13 14 15 22 23 24 0 0 0
               7 8 9 16 17 18 25 26 27 0 0 0

How do I dispose of A(:,:,4) all together?

Thanks

Subject: Removing array dimension

From: dpb

Date: 1 May, 2013 01:35:25

Message: 5 of 9

On 4/30/2013 8:26 PM, Jonathan W Smith wrote:
> dpb <none@non.net> wrote in message <klplip$grr$1@speranza.aioe.org>...
...

>> Well, what do you want to happen to which planes of the 5? IOW, what
>> is the content of the resultant array just the content of the first
>> plane?
>>
>> If so, simply
>>
>> A(:,:,2:5)=[];
>>
>> Salt to suit or provide more reqm'ts if there are any...
....

> Lets say A is an array with dimensions 3 x 3 x 4.
> A(:,:,1) = 1 2 3
> A(:,:,2) = 10 11 12
 > A(:,:,3) = 18 20 21
 > A(:,:,4) = 28 29 30 4 5 6 13 14
> 15 22 23 24 31 32 33
> 7 8 9 16 17 18 25 26 27 34 35 36
...

Makes no sense to me...if size(A)=[3,3,4], then A(:,:,1) has size()=3,3
and you show only 3 elements not 9.

Please 'splain away the mismatch.

--

Subject: Removing array dimension

From: Nasser M. Abbasi

Date: 1 May, 2013 04:17:40

Message: 6 of 9

On 4/30/2013 8:26 PM, Jonathan W Smith wrote:


> Lets say A is an array with dimensions 3 x 3 x 4.
> A(:,:,1) = 1 2 3 A(:,:,2) = 10 11 12 A(:,:,3) = 18 20 21 A(:,:,4) = 28 29 30
> 4 5 6 13 14 15 22 23 24 31 32 33
> 7 8 9 16 17 18 25 26 27 34 35 36
>
> When I input A(:,:,4) = [] I get the following
>
> A(:,:,1) = 1 2 3 A(:,:,2) = 10 11 12 A(:,:,3) = 18 20 21 A(:,:,4) = 0 0 0
> 4 5 6 13 14 15 22 23 24 0 0 0
> 7 8 9 16 17 18 25 26 27 0 0 0
>
> How do I dispose of A(:,:,4) all together?
>
> Thanks
>

You have something wrong. Can you do clear all, and start again? and paste
the actual input and output as is from the console?

EDU>> A=rand(3,3,4);
EDU>> size(A)
      3 3 4

EDU>> A(:,:,4)=[];
EDU>> size(A)
      3 3 3

So it works as expected.

To remove the 3rd dimentsion all togother, then simply do,
assuming in this example you want to keep the first page:

EDU>> A(:,:,2:end)=[];
EDU>> size(A)
ans =
      3 3

The above is the same as saying

   squeeze(A);

To remove the third dimension, and now keep the second page,
then do

B=A(:,:,2);
A=B

etc..

--Nasser

Subject: Removing array dimension

From: Nasser M. Abbasi

Date: 1 May, 2013 04:24:05

Message: 7 of 9


> EDU>> A(:,:,2:end)=[];
> EDU>> size(A)
> ans =
> 3 3
>
> The above is the same as saying
>
> squeeze(A);
>

opps, that should be

      squeeze(A(:,:,1));

> To remove the third dimension, and now keep the second page,
> then do
>
> B=A(:,:,2);
> A=B
>

or just B=squeeze(A(:,:,2)) ofcourse..

--Nasser

Subject: Removing array dimension

From: Jonathan W Smith

Date: 1 May, 2013 12:47:07

Message: 8 of 9

dpb <none@non.net> wrote in message <klprgt$t7m$1@speranza.aioe.org>...
> On 4/30/2013 8:26 PM, Jonathan W Smith wrote:
> > dpb <none@non.net> wrote in message <klplip$grr$1@speranza.aioe.org>...
> ...
>
> >> Well, what do you want to happen to which planes of the 5? IOW, what
> >> is the content of the resultant array just the content of the first
> >> plane?
> >>
> >> If so, simply
> >>
> >> A(:,:,2:5)=[];
> >>
> >> Salt to suit or provide more reqm'ts if there are any...
> ....
>
> > Lets say A is an array with dimensions 3 x 3 x 4.
> > A(:,:,1) = 1 2 3
> > A(:,:,2) = 10 11 12
> > A(:,:,3) = 18 20 21
> > A(:,:,4) = 28 29 30 4 5 6 13 14
> > 15 22 23 24 31 32 33
> > 7 8 9 16 17 18 25 26 27 34 35 36
> ...
>
> Makes no sense to me...if size(A)=[3,3,4], then A(:,:,1) has size()=3,3
> and you show only 3 elements not 9.
>
> Please 'splain away the mismatch.
>
> --
dpb
The arrays above that I typed did not come out correctly on your end. I apologize. Nassar answered my question. I would appreciate if the responses were just responses. Some of the responses seem to have a streak of elitism and arrogance. I think most people try to explain their problem the best way they know how.

Thanks Jonathan

Subject: Removing array dimension

From: Steven_Lord

Date: 1 May, 2013 13:55:22

Message: 9 of 9



"Jonathan W Smith" <jwsmith9@gmail.com> wrote in message
news:klpqvj$fva$1@newscl01ah.mathworks.com...
> dpb <none@non.net> wrote in message <klplip$grr$1@speranza.aioe.org>...
>> On 4/30/2013 5:28 PM, Jonathan W Smith wrote:

*snip*

> Lets say A is an array with dimensions 3 x 3 x 4. A(:,:,1) = 1 2 3
> A(:,:,2) = 10 11 12 A(:,:,3) = 18 20 21 A(:,:,4) = 28 29 30 4 5
> 6 13 14 15 22 23 24
> 31 32 33
> 7 8 9 16 17 18 25 26
> 27 34 35 36
>
> When I input A(:,:,4) = [] I get the following
>
> A(:,:,1) = 1 2 3 A(:,:,2) = 10 11 12 A(:,:,3) = 18 20 21
> A(:,:,4) = 0 0 0 4 5 6 13 14 15
> 22 23 24 0 0 0
> 7 8 9 16 17 18 25 26
> 27 0 0 0

That's not what I see.

A = permute(reshape(1:36, [3 3 4]), [2 1 3]);
B = A; B(:, :, 4) = []; szB = size(B) % [3 3 3]
C = A; C(:, :, 4) = 0; szC = size(C ) % [3 3 4] with 4th page all zero
D = A; x = []; D(:, :, 4) = x % Error

Unless A is an object and you (or the object author) have overriden
indexing, or if you've overloaded indexing for double (Bad Idea!) there's
something odd going on or there's a difference between what you posted and
what you actually executed in MATLAB. Please post the exact lines of code
with which you generate your A array and the exact indexing expression that
results in the array with an all-zero 4th page.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us