[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: syntax help: simple formula expansion (sub-indexes with vectors)
From: |
Paul Kienzle |
Subject: |
Re: syntax help: simple formula expansion (sub-indexes with vectors) |
Date: |
Thu, 3 Jun 2004 12:05:46 -0400 |
The simplest way is to do the elements separately:
a = h/n * ([1:n]' + 0.5);
d = [ I(1)+a*x(1), I(2)+a*x(2)];
If d were a k-vector rather than a 2-vector, then put it in a loop:
d = zeros(n,k);
for i=1:k, d(:,k) = I(k) + a*x(k); end
You can do it without the for-loop using:
d = ones(n,1)*I + (a*ones(1,k)) .* (ones(n,1)*x);
but that increases memory usage, and may actually be slower.
Paul Kienzle
address@hidden
On Jun 3, 2004, at 11:46 AM, edA-qa mort-ora-y wrote:
Although I've been working with octave for a little while now I am
still having a lot of problems working with the most basic
expressions. Can somebody please indicate to me the prefered way to
handle the below example.
Given these variables (not in octave notation to avoid confusion):
l is a 2-space vector (ex. (4,5), in octave: [4,5])
x is a 2-space vector
h is a real number
n is an integer
i is the range of numbers [1,n]
I want to evalute the following function for every integer in i:
d(i) = l + ( h/n * ( i + 0.5 ) ) * x
I want to create the variable d that has the result of the function
for each element of i. I try doing something like below, but I keep
getting errors.
l=[1,1];
x=[0,1];
h=6;
n=3;
i=1:n
for q=i; d(q)=l-((h/n)*(q+0.5))*x; end
error: A(I) = X: X must be a scalar or a vector with same length as I
How do I do this? (If possible it'd be nice not to use a loop, but
since I can't even get the loop working I feel unqualified to find a
better solution)
--
edA-qa mort-ora-y
Idea Architect
http://disemia.com/
-------------------------------------------------------------
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
-------------------------------------------------------------
-------------------------------------------------------------
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
-------------------------------------------------------------