[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
## Re: Novice question (matrix initialization)

**From**: |
John Eaton |

**Subject**: |
Re: Novice question (matrix initialization) |

**Date**: |
Mon, 28 Feb 94 13:12:18 CST |

address@hidden (Przemek Klosowski) wrote:
: I am a complete novice to matlab language (actually I use octave). I
: am playing with Savitzky-Golay filters, which are calculated from a
: rather simple matrix: A(i,j) = i^j where i=-n:n and j=0:m. I can set
: up this matrix by writing two loops, but I am sure that there is a
: neater way, something like
:
: AA = m columns of [-n, -n+1 ... n-1, n]
: AB = 2n+1 rows of [ 0, 1, ... m-1 ]
: A = AA .^ AB;
:
: Unfortunately, only the last statement is a valid octave/matlab code.
: Can anyone suggest how should I elegantly write first two lines?
Here are two ways I can think of. The first uses a simple
multiplication:
AA = (-n:n)' * ones (1, m)
AB = ones (2*n+1, 1) * (0:m-1)
and the other uses obscure indexing tricks:
v1 = (-n:n)'
AA = v1 (:, ones (1, m))
v2 = 0:m-1
AB = v2 (ones (1, 2*n+1), :)
I have often seen people claim that Matlab handles this faster because
it avoids multiplication.
In some cases, the indexing trick may be faster in Octave as well, but
for small matrices, I suspect that both operations require about the
same amount of time, because there is quite a bit of overhead for
doing this sort of index calculation.
The multiplication seems much clearer (to me anyway) and does not
require a temporary variable (Matlab and Octave can only handle
indexing of variable, not arbitrary expressions).
jwe