Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Cut out from Multidimensional Array

15 views
Skip to first unread message

Philipp H

unread,
Jun 30, 2009, 7:15:04 AM6/30/09
to
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

Bruno Luong

unread,
Jun 30, 2009, 7:59:02 AM6/30/09
to
"Philipp H" <sau...@gmx.de> wrote in message <h2cs3o$hfn$1...@fred.mathworks.com>...

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

Bruno

Philipp H

unread,
Jun 30, 2009, 8:39:01 AM6/30/09
to
"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <h2cum6$1kb$1...@fred.mathworks.com>...

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

Bruno Luong

unread,
Jun 30, 2009, 8:59:02 AM6/30/09
to
"Philipp H" <sau...@gmx.de> wrote in message <h2d115$ka6$1...@fred.mathworks.com>...

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

Yanai

unread,
Jun 30, 2009, 9:03:02 AM6/30/09
to
"Philipp H" <sau...@gmx.de> wrote in message <h2cs3o$hfn$1...@fred.mathworks.com>...

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

Philipp H

unread,
Jun 30, 2009, 9:20:02 AM6/30/09
to
"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <h2d26m$o1d$1...@fred.mathworks.com>...


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

0 new messages