help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how can I hold matrices in a container?


From: Jaroslav Hajek
Subject: Re: how can I hold matrices in a container?
Date: Thu, 28 May 2009 06:26:46 +0200

On Wed, May 27, 2009 at 11:31 PM, David Bateman <address@hidden> wrote:
> Dr. Johannes Zellner wrote:
>> Hi
>>
>> I'd like to do an operation like
>>
>>     [A, B, C] .* [x, y, z]
>>
>> fast! A, B, C are matrices, x, y, z are scalar.
>> The result should be simply a row of the scaled matrices like this:
>>
>>     [A .* x, B .* y, C .* z]
>>
>> Apparently, holding [A, B, C] isn't possible.
>>
>> What's the recommended way to do this fast?
>>
>>
>
>  cellfun(@(x, y) x .* y, {A, B, C}, {x, y, z}, 'UniformOutput', false)
>
>

Plus my usual comment,

cellfun(@times, {A, B, C}, {x, y, z}, 'UniformOutput', false)

might be slightly better, as it bypasses the interpreter in the inner
loop. Though this is probably negligible if the matrices are large
enough.

If your matrices are of equal size, you can also construct a 3D array,
reshape it to a matrix, use row scaling, and reshape back. Note that
reshape is a constant-time operation, so this is going to be very
fast.

U = cat (3, A, B, C);
w = [x y z];
U = reshape (reshape (U, [], 3) * diag (w), size (U));

hth

-- 
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

[Prev in Thread] Current Thread [Next in Thread]