Thanks again!
I was mixing up my rowcolumn settings.
A small comparison between the different approaches
The loop with bsxfun (abs, transpose outside): Elapsed time is 0.051334 seconds.
absB = abs(B) .';
for k=1:length(c)
A(m,k) = sum (bsxfun ( @power, absB, c(k) ) );
end
The basic loop (abs outside): Elapsed time is 0.054493 seconds.
absB = abs(B);
for k=1:length(c)
A(m,k) = sum (absB .^ c(k));
end
The vectorized form: Elapsed time is 0.049022 seconds.
absB = abs(B) .'; %note transpose
c = [0.5 1 2 4 8];
A(m,:) = sum(bsxfun(@power, absB, c));
Another run gave
Elapsed time is 0.051442 seconds.
Elapsed time is 0.054468 seconds.
Elapsed time is 0.055072 seconds.
So there is really not that much of a difference between the approaches per calculation. However, it does seem that the two loop options are stable in calculation time, while the vectorized form changes between cases.
to study this a bit more I let my entire loop run, and averaged the times for all of them. The calculcations become shorter as my loops progress, hence the averages are much lower than the numbers above.
1. bsxfun loop 0.0158 + 0.009 total time 15.6885
2. basic loop 0.0163 + 0.0093 16.0933
3. vectorized bsxfun 0.0166 + 0.0097 16.4012
Interesting. The bsxfun is good in any case.
Thanks again for your help.
Daphne
Walter Roberson <roberson@hushmail.com> wrote in message <phs4l.4344$hr3.4255@newsfe01.iad>...
> Daphne wrote:
>
> > bsxfun and then found that if the B and c are not the same length it will not work?
>
> You must have missed the transpose. For your problem, one input to bsxfun should
> be a row vector and the other input should be a column vector. It doesn't matter which
> is the row and which is the column: if B is the row and C is the column, the result would
> be the transpose of the case where B is the column and C is the row. I already took
> the choice of which should be row or column into account when I posted the previous code.
>
> Quoting my previous solution:
>
> absB = abs(B) .'; %note transpose
> c = [0.5 1 2 4 8];
> A(m,:) = sum(bsxfun(@power, absB, c));
>
> However if B is already a column vector do not put the transpose on the end
> of the first line.
>
> Your hybrid solution of doing bsxfun in a loop is going to be slower than
> doing bsxfun the way I describe: when you pass in c(k) as the only thing
> for the final argument, you waste the power of bsxfun, and you would probably
> find
>
> absB = abs(B);
> for k=1:length(c)
> A(m,k) = sum (absB .^ c(k));
> end
>
> to be faster. This is similar to your original solution except it pulls the
> absolute value taking out of the loop.
>
>
> Consider your hybrid solution again:
>
> absB = abs(B);
> for k=1:length(c)
> A(m,k) = sum (bsxfun ( @power, absB.', c(k) ) );
> end
>
> You are taking the transpose of absB each time through the loop. That's a
> waste of time. Pull it out:
>
> absB = abs(B) .';
> for k=1:length(c)
> A(m,k) = sum (bsxfun ( @power, absB, c(k) ) );
> end
>
> This should be faster than your hybrid version, slower than the loop that does
> not use bsxfun at all, and my original solution would probably be fastest.
>
> 
> .signature note: I am now avoiding replying to unclear or ambiguous postings.
> Please review questions before posting them. Be specific. Use examples of what you mean,
> of what you don't mean. Specify boundary conditions, and data classes and value
> relationships  what if we scrambled your data or used Inf, NaN, or complex(rand,rand)?
