Thread Subject: Cut out from Multidimensional Array

Subject: Cut out from Multidimensional Array

From: Philipp H

Date: 30 Jun, 2009 11:15:04

Message: 1 of 6

What i would like to do is a for loop over a n-dimensional array. So I thought about a recursive function with a for loop like that:

function A=go_crazy(A)

if length(A) >1
  for i=1:d
    A(i,:,:,:,:)=i*go_crazy(A(i,:,:,:));
  end
end


Problem is that I don't know in advance how many : to make. So my question is can i cut out an n-1 dimensional array without knowing n :)

Thanks alot

Subject: Cut out from Multidimensional Array

From: Bruno Luong

Date: 30 Jun, 2009 11:59:02

Message: 2 of 6

"Philipp H" <saucool@gmx.de> wrote in message <h2cs3o$hfn$1@fred.mathworks.com>...
> What i would like to do is a for loop over a n-dimensional array. So I thought about a recursive function with a for loop like that:
>
> function A=go_crazy(A)
>
> if length(A) >1
> for i=1:d
> A(i,:,:,:,:)=i*go_crazy(A(i,:,:,:));
> end
> end
>
>
> Problem is that I don't know in advance how many : to make. So my question is can i cut out an n-1 dimensional array without knowing n :)
>
> Thanks alot

Something like this:
c(1:3)='{:}'
A(1,c{:})

Bruno

Subject: Cut out from Multidimensional Array

From: Philipp H

Date: 30 Jun, 2009 12:39:01

Message: 3 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h2cum6$1kb$1@fred.mathworks.com>...
> "Philipp H" <saucool@gmx.de> wrote in message <h2cs3o$hfn$1@fred.mathworks.com>...
> > What i would like to do is a for loop over a n-dimensional array. So I thought about a recursive function with a for loop like that:
> >
> > function A=go_crazy(A)
> >
> > if length(A) >1
> > for i=1:d
> > A(i,:,:,:,:)=i*go_crazy(A(i,:,:,:));
> > end
> > end
> >
> >
> > Problem is that I don't know in advance how many : to make. So my question is can i cut out an n-1 dimensional array without knowing n :)
> >
> > Thanks alot
>
> Something like this:
> c(1:3)='{:}'
> A(1,c{:})
>
> Bruno

doesn't work. And I have no clue what it means at all

Subject: Cut out from Multidimensional Array

From: Bruno Luong

Date: 30 Jun, 2009 12:59:02

Message: 4 of 6

"Philipp H" <saucool@gmx.de> wrote in message <h2d115$ka6$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h2cum6$1kb$1@fred.mathworks.com>...
> > "Philipp H" <saucool@gmx.de> wrote in message <h2cs3o$hfn$1@fred.mathworks.com>...
> > > What i would like to do is a for loop over a n-dimensional array. So I thought about a recursive function with a for loop like that:
> > >
> > > function A=go_crazy(A)
> > >
> > > if length(A) >1
> > > for i=1:d
> > > A(i,:,:,:,:)=i*go_crazy(A(i,:,:,:));
> > > end
> > > end
> > >
> > >
> > > Problem is that I don't know in advance how many : to make. So my question is can i cut out an n-1 dimensional array without knowing n :)
> > >
> > > Thanks alot
> >
> > Something like this:
> > c(1:3)='{:}'
> > A(1,c{:})
> >
> > Bruno
>
> doesn't work. And I have no clue what it means at all

Ah sorry there is a typo it should be
 A=rand(2,1,3);

clear c; c(1:ndims(A)-1)={':'};
A(1,c{:})

Bruno

Subject: Cut out from Multidimensional Array

From: Yanai

Date: 30 Jun, 2009 13:03:02

Message: 5 of 6

"Philipp H" <saucool@gmx.de> wrote in message <h2cs3o$hfn$1@fred.mathworks.com>...
> What i would like to do is a for loop over a n-dimensional array. So I thought about a recursive function with a for loop like that:
>
> function A=go_crazy(A)
>
> if length(A) >1
> for i=1:d
> A(i,:,:,:,:)=i*go_crazy(A(i,:,:,:));
> end
> end
>
>
> Problem is that I don't know in advance how many : to make. So my question is can i cut out an n-1 dimensional array without knowing n :)
>
> Thanks alot

res=1;
for k=1:numel(A)
   res = res * A(k)
end

Subject: Cut out from Multidimensional Array

From: Philipp H

Date: 30 Jun, 2009 13:20:02

Message: 6 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h2d26m$o1d$1@fred.mathworks.com>...
> "Philipp H" <saucool@gmx.de> wrote in message <h2d115$ka6$1@fred.mathworks.com>...
> > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h2cum6$1kb$1@fred.mathworks.com>...
> > > "Philipp H" <saucool@gmx.de> wrote in message <h2cs3o$hfn$1@fred.mathworks.com>...
> > > > What i would like to do is a for loop over a n-dimensional array. So I thought about a recursive function with a for loop like that:
> > > >
> > > > function A=go_crazy(A)
> > > >
> > > > if length(A) >1
> > > > for i=1:d
> > > > A(i,:,:,:,:)=i*go_crazy(A(i,:,:,:));
> > > > end
> > > > end
> > > >
> > > >
> > > > Problem is that I don't know in advance how many : to make. So my question is can i cut out an n-1 dimensional array without knowing n :)
> > > >
> > > > Thanks alot
> > >
> > > Something like this:
> > > c(1:3)='{:}'
> > > A(1,c{:})
> > >
> > > Bruno
> >
> > doesn't work. And I have no clue what it means at all
>
> Ah sorry there is a typo it should be
> A=rand(2,1,3);
>
> clear c; c(1:ndims(A)-1)={':'};
> A(1,c{:})
>
> Bruno


Thanks alot Bruno. You really helped me. I'm still puzzled over the syntax but it works :D

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
multidimensiona... Philipp H 30 Jun, 2009 07:19:05
rssFeed for this Thread

Contact us at files@mathworks.com