A problem with grammar

3 views (last 30 days)
Karel
Karel on 26 Jul 2012
Here is a function I wrote, with N beeing a scalar (example 6) and x a vector (example [0;1;2;3;4;5]). Y1 gets input as 0 in the example and Y2 as (36-x^2)
function [ sort1 , sort2 ] = front(N,x)
Y1=input('frontière inférieure: ','s') ;
s1=vectorize(Y1);
Y2=input('frontière supérieure: ','s');
s2=vectorize(Y2);
sort1= ones(1,N+1);
for d=1:1:N;
b=x(d);
a=s1(b);
sort1(d)=a;
end
sort2= ones(1,N+1);
for d=1:1:N;
b=x(d);
a=s2(b);
sort2(d)=a;
end
end
My problem are the lines a=s1(b) and a=s2(b). My goal is to inject the values of the vector x one by one in the expressions s1 and s2, but: 1)I am not sure my semantics are correct? 2)How does this program reacts when it receives a scalar instead of an expression?
This is what I get when i run the program:
frontière inférieure: 0
frontière supérieure: 36-x^2
??? Attempted to access s1(0); index must be a positive integer
or logical.
Error in ==> front at 12
a=s1(b);

Accepted Answer

Davide Ferraro
Davide Ferraro on 26 Jul 2012
Not sure that I've fully understood your goal. From what I've got you may try to use the EVAL function to evaluate your string. This is not the most elegant approach but can be a starting point:
function [ sort1 ] = front(N,x)
Y1=input('frontière inférieure: ','s') ;
s1=vectorize(Y1);
if ~isnan(str2double(s1))
sort1 = repmat(eval(s1),1,N+1);
else
sort1 = eval(s1);
end
end
There's margin to improve the code:

More Answers (4)

Image Analyst
Image Analyst on 26 Jul 2012
Yeah, a little problem with grammar - we use "syntax" when talking about that in regards to computer language rather than "grammar" or "semantics." But the problem is not a syntax problem - the real problem is that you're passing in x, which has a zero as one of its elements and you're trying to use that as an index for s1. You can't have zero as an index. So fix your x to have just positive integers, or make it a logical vector, and you should be okay.

Karel
Karel on 26 Jul 2012
I thank you for your answer, but it really is a syntax problem: s1 is never a vector, it is either a number (0) or an equation (36-x^2). In the case of a number, i just need a vector size n+1 composed of the number, and in the case of an equation, I need the result to be a vector size n+1, with each element of the vector beeing the solution of the equation for a value of the vector x

Karel
Karel on 26 Jul 2012
Ill try to explain it better:
so i have this function
function [ sort1 , sort2 ] = front(x,N)
Y1=input('frontière inférieure (y1(x)): ','s') ;
s1=vectorize(Y1);
Y2=input('frontière supérieure (y2(x)): ','s');
s2=vectorize(Y2);
sort1= ones(1,N+1);
for d=1:1:N;
b=x(d);
a=s1(b);
sort1(d)=a;
end
sort2= ones(1,N+1);
for d=1:1:N;
b=x(d);
a=s2(b);
sort2(d)=a;
end
end
x is a vector. The example given is this one [0,1,2,3,4]
N is a number. 5 in the example
The function will ask the user to enter two fronteers. These can either be numbers (example:0) or equations (example:36-x^2)
What I want to achieve is:
if the fronteer is a number, create a vector sort composed soley of that number
if the fronteer is an equation, create a vector sort in which each element is the solution of the equation when the unknow value (x) is replaced by the value of the x vector in that place.
Example: x=[0,1,2,3,4]
N=5
y1=0
y2= 36-x^2
SOLUTION
sort 1= [0,0,0,0,0]
sort 2= [36,35,32,27,20]

Image Analyst
Image Analyst on 26 Jul 2012
x=[0,1,2,3,4]
so,
b=x(d);
and N = 5, that means b will equal 0, 1, 2, 3, and 4 as the loop iterates. OK, let's look at the first iteration. You say
a=s1(b);
but b = x(1) in the first iteration, which means b = 0 because x(1) = 0. So now you're saying
a = s1(0);
That's just not allowed. You can't have 0 as an index. I hope it's clear now.

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!