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:
How to retrieve the given column of the data of arbitrary size?

Subject: How to retrieve the given column of the data of arbitrary size?

From: Ha

Date: 12 Nov, 2012 12:25:16

Message: 1 of 8


For instance, A is a 10x10x10x10 matrix, the 3rd column of the 2nd dimension of A is given by squeeze(A(:,3,:,:)). What if the size of A is arbitrary? A could be 10x10, 5x10x5, 10x10x5x5x10, ..., or anything else. How to write a function applicable to arbitray A?

Subject: How to retrieve the given column of the data of arbitrary size?

From: Justin Ashmall

Date: 12 Nov, 2012 12:37:15

Message: 2 of 8

> For instance, A is a 10x10x10x10 matrix, the 3rd column of the 2nd dimension of A is given by squeeze(A(:,3,:,:)). What if the size of A is arbitrary? A could be 10x10, 5x10x5, 10x10x5x5x10, ..., or anything else. How to write a function applicable to arbitray A?

Take a look at the function subsref. You'll need to build an appropriate input structure (S) with the right number of colons. It shouldn't be difficult, just fiddly. The documentation will point you in the right direction.

Justin

Subject: How to retrieve the given column of the data of arbitrary size?

From: Ha

Date: 13 Nov, 2012 08:13:12

Message: 3 of 8

"Justin Ashmall" wrote in message <k7qqhr$jf7$1@newscl01ah.mathworks.com>...
> > For instance, A is a 10x10x10x10 matrix, the 3rd column of the 2nd dimension of A is given by squeeze(A(:,3,:,:)). What if the size of A is arbitrary? A could be 10x10, 5x10x5, 10x10x5x5x10, ..., or anything else. How to write a function applicable to arbitray A?
>
> Take a look at the function subsref. You'll need to build an appropriate input structure (S) with the right number of colons. It shouldn't be difficult, just fiddly. The documentation will point you in the right direction.
>
> Justin


Thanks.

Subject: How to retrieve the given column of the data of arbitrary size?

From: Bruno Luong

Date: 13 Nov, 2012 13:00:22

Message: 4 of 8

"Ha" wrote in message <k7qprc$h2m$1@newscl01ah.mathworks.com>...
>
> For instance, A is a 10x10x10x10 matrix, the 3rd column of the 2nd dimension of A is given by squeeze(A(:,3,:,:)). What if the size of A is arbitrary? A could be 10x10, 5x10x5, 10x10x5x5x10, ..., or anything else. How to write a function applicable to arbitray A?

A = rand(2,3,4,5);

n = ndims(A);
ind(1:n) = {':'};
ind{2} = 3; % ind = {':' 3 ':' ':' }
 A3=squeeze(A(ind{:}))

% Bruno

Subject: How to retrieve the given column of the data of arbitrary size?

From: Jan Simon

Date: 13 Nov, 2012 13:24:11

Message: 5 of 8

Dear Bruno,
> ind = {':', 3, ':', ':' };
> A3=squeeze(A(ind{:}))

I like this method. It is used in Matlab's toolbox functions, but is it documented also?

Kind regards, Jan

Subject: How to retrieve the given column of the data of arbitrary size?

From: Ha

Date: 13 Nov, 2012 14:54:17

Message: 6 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k7tg96$lq8$1@newscl01ah.mathworks.com>...
> "Ha" wrote in message <k7qprc$h2m$1@newscl01ah.mathworks.com>...
> >
> > For instance, A is a 10x10x10x10 matrix, the 3rd column of the 2nd dimension of A is given by squeeze(A(:,3,:,:)). What if the size of A is arbitrary? A could be 10x10, 5x10x5, 10x10x5x5x10, ..., or anything else. How to write a function applicable to arbitray A?
>
> A = rand(2,3,4,5);
>
> n = ndims(A);
> ind(1:n) = {':'};
> ind{2} = 3; % ind = {':' 3 ':' ':' }
> A3=squeeze(A(ind{:}))
>
> % Bruno

Haha. I have writen a function in this manner.

Subject: How to retrieve the given column of the data of arbitrary size?

From: Steven_Lord

Date: 14 Nov, 2012 15:15:42

Message: 7 of 8



"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message
news:k7thlr$qm2$1@newscl01ah.mathworks.com...
> Dear Bruno,
>> ind = {':', 3, ':', ':' };
>> A3=squeeze(A(ind{:}))
>
> I like this method. It is used in Matlab's toolbox functions, but is it
> documented also?

I don't believe it's explicitly documented. This is about as close as the
documentation gets to describing that behavior.

http://www.mathworks.com/help/matlab/matlab_oop/indexed-reference-and-assignment.html#br09e97-1

" A colon used as an index is passed in the cell array as the string ':'. "

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

Subject: How to retrieve the given column of the data of arbitrary size?

From: Paul

Date: 18 Nov, 2012 23:14:11

Message: 8 of 8

"Steven_Lord" <slord@mathworks.com> wrote in message <k80civ$n39$1@newscl01ah.mathworks.com>...
>
>
> "Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message
> news:k7thlr$qm2$1@newscl01ah.mathworks.com...
> > Dear Bruno,
> >> ind = {':', 3, ':', ':' };
> >> A3=squeeze(A(ind{:}))
> >
> > I like this method. It is used in Matlab's toolbox functions, but is it
> > documented also?
>
> I don't believe it's explicitly documented. This is about as close as the
> documentation gets to describing that behavior.
>
> http://www.mathworks.com/help/matlab/matlab_oop/indexed-reference-and-assignment.html#br09e97-1
>
> " A colon used as an index is passed in the cell array as the string ':'. "
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

One would think that this behavior (and any other aspects of indexing for Matlab arrays) should be documented here:
http://www.mathworks.com/help/matlab/math/matrix-indexing.html

Also, (at least in earlier versions of Matlab), indexing with characters is allowed, like this:
A('a') and A('ab'). Since that wasn't documented, it's not clear if that's supposed to be legal syntax. Further confusing the issue is that A('a') and A(':') are treated inconsistently. The former returns a single element of A (converting 'a' to its ASCII code I believe) and the latter returns all of A.

Tags for this Thread

No tags are associated with 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