[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
without a for loop? (was "compiling .m files")
From: |
Mike Miller |
Subject: |
without a for loop? (was "compiling .m files") |
Date: |
Tue, 12 Nov 2002 20:07:03 -0600 (CST) |
On Wed, 13 Nov 2002, Hein Zelle wrote:
> Unrelated question: is there a way to do the following without using a
> for loop?
>
> a = [1, 2; 3, 4];
> b = mean(a);
>
> for r = 1:2
> anomaly(r,:) = a(r,:) - b
> endfor
>
> I would like to use the : notation to indicate that I want the row
> vector b to be subtracted from each row of a, but i can't figure out how
> to do it.
How about this?:
anomaly = a - ones(size(a,1),1)*b ;
In general, you could do this for any matrix X:
Y = X - ones(size(X,1),1) * mean(X)
Then mean(Y) = zeros(1,size(X,2)), always.
Mike
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------