From: PL on
Hi,
I'm looking for a way to remove a for loop. a is a matrix with know integer values from 1 to n. I need to do separate operations for each value of n. n is a big number and a is a big matrix, so it consumes a lot of time now.

What I want is transforming this:

for i=1:n
a=(b==i);
(operations on a)
end

into something similar like this:

(compute a(i))
for i=1:n
(operations on a(i))
end

Is the a way to compute
for i=1:n
a=(b==i);
end

faster?

Thanks
From: us on
"PL " <pluc_2254(a)hotmail.com> wrote in message <i1l2qv$2f2$1(a)fred.mathworks.com>...
> Hi,
> I'm looking for a way to remove a for loop. a is a matrix with know integer values from 1 to n. I need to do separate operations for each value of n. n is a big number and a is a big matrix, so it consumes a lot of time now.
>
> What I want is transforming this:
>
> for i=1:n
> a=(b==i);
> (operations on a)
> end
>
> into something similar like this:
>
> (compute a(i))
> for i=1:n
> (operations on a(i))
> end
>
> Is the a way to compute
> for i=1:n
> a=(b==i);
> end
>
> faster?
>
> Thanks

there's stuff like

help arrayfun; % <- and siblings...
% however, while syntactically more pretty, they are rarely faster
% also, there's the possibility of MEXing ML code...

us
From: Alan B on
"us " <us(a)neurol.unizh.ch> wrote in message <i1l4j0$r07$1(a)fred.mathworks.com>...
> "PL " <pluc_2254(a)hotmail.com> wrote in message <i1l2qv$2f2$1(a)fred.mathworks.com>...
> > Hi,
> > I'm looking for a way to remove a for loop. a is a matrix with know integer values from 1 to n. I need to do separate operations for each value of n. n is a big number and a is a big matrix, so it consumes a lot of time now.
> >
> > What I want is transforming this:
> >
> > for i=1:n
> > a=(b==i);
> > (operations on a)
> > end
> >
> > into something similar like this:
> >
> > (compute a(i))
> > for i=1:n
> > (operations on a(i))
> > end
> >
> > Is the a way to compute
> > for i=1:n
> > a=(b==i);
> > end
> >
> > faster?
> >
> > Thanks
>
> there's stuff like
>
> help arrayfun; % <- and siblings...
> % however, while syntactically more pretty, they are rarely faster
> % also, there's the possibility of MEXing ML code...
>
> us

Am I missing something? Isn't this just:
a = b==(1:n);

OP doesn't seem to be asking about the rest of the "operations on a", just that first line inside the loop.
From: us on
"Alan B"
> Am I missing something? Isn't this just:
> a = b==(1:n);
>
> OP doesn't seem to be asking about the rest of the "operations on a", just that first line inside the loop.

well... let's assume it isn't THAT simple...

:-)
us
From: PL on
"Alan B" <monguin61REM(a)OVETHIS.yahoo.com> wrote in message <i1l647$bhv$1(a)fred.mathworks.com>...
> "us " <us(a)neurol.unizh.ch> wrote in message <i1l4j0$r07$1(a)fred.mathworks.com>...
> > "PL " <pluc_2254(a)hotmail.com> wrote in message <i1l2qv$2f2$1(a)fred.mathworks.com>...
> > > Hi,
> > > I'm looking for a way to remove a for loop. a is a matrix with know integer values from 1 to n. I need to do separate operations for each value of n. n is a big number and a is a big matrix, so it consumes a lot of time now.
> > >
> > > What I want is transforming this:
> > >
> > > for i=1:n
> > > a=(b==i);
> > > (operations on a)
> > > end
> > >
> > > into something similar like this:
> > >
> > > (compute a(i))
> > > for i=1:n
> > > (operations on a(i))
> > > end
> > >
> > > Is the a way to compute
> > > for i=1:n
> > > a=(b==i);
> > > end
> > >
> > > faster?
> > >
> > > Thanks
> >
> > there's stuff like
> >
> > help arrayfun; % <- and siblings...
> > % however, while syntactically more pretty, they are rarely faster
> > % also, there's the possibility of MEXing ML code...
> >
> > us
>
> Am I missing something? Isn't this just:
> a = b==(1:n);
>
> OP doesn't seem to be asking about the rest of the "operations on a", just that first line inside the loop.

Indeed, but your line is not working (at least, for my needs):
a=floor(5*rand(5)+1);
b = a==(1:5)

??? Error using ==> eq
Matrix dimensions must agree.