Problem in defining vector variable using ellipsis when code requires more than one line

I have a function which requires a vector variable y to be defined. The vector has 3 rows - y(1), y(2) & y(3), each defined by long equations which require more than one line of coding. When I use ellipsis at the end of a line I get an error message:- "unable to perform assignment because the size of the left side is the size of the right hand side did not match the size of the indexing on the left hand side". I am sure this is because the use of ellipsis adds spaces and this is interpreted as Y having a greater number of rows than 3. For example, if I define y(1), y(2) and y(3) each using 2 lines of coding using ellipsis then the vector y is probably interpreted as having 6 rows, which is incorrect.
I get the error message with the following simple example where I split the first line of code. Note that it is not necessary to split the first line of coding in this example but it is done to show the problem that results.
%Nested function that computes the objective function
function y = nestedfun(x)
y(1) = [(a*x(1))+(b*(x(2))^2)...
-(a*(x(3))^2)+14];
y(2)=[(a*(x(1))^2)+(b*x(2))-(b*(x(3))^2)+24];
y(3)=[(b*(x(1))^2)-(c*(x(2))^2)+(a*x(3))-19];

 采纳的回答

y(1) = [(a*x(1))+(b*(x(2))^2)...
-(a*(x(3))^2)+14];
is not the same as
y(1) = [(a*x(1))+(b*(x(2))^2)-(a*(x(3))^2)+14];
Instead it is the same as
y(1) = [(a*x(1))+(b*(x(2))^2) -(a*(x(3))^2)+14];
which defines two array elements.
Spacing matters.
The easiest way to repair this is to change the code to
y(1) = [(a*x(1))+(b*(x(2))^2)...
- (a*(x(3))^2)+14];

3 个评论

Thanks very much Walter. I realised that the array element number was being increased due to the gaps but couldn't figure out why. I haven't used matlab for a long time but don't remember having come across a gap issue before. Also thanks for editing the question to present the code in the proper fashion - I wasn't sure how to do that. Your assistance is greatly appreciated.
When you use
line1...
line2
then everything starting from the ... is removed from the end of the first line, and everything from the second line is put starting where the ... was. In this example, there are three spaces at the beginning of line2, and they are included in what is put at the end of line1
line1 line2
If the spaces had not been there, if the input had been
line1...
line2
then MATLAB would have treated it as
line1line2
The ... operator is not treated as whitespace: if you do not happen to give whitespace before the ... operator or at the beginning of the next line, then there will not be any whitespace in what gets joined together.
Now, consider then
[1...
-2]
This will get processed as
[1 -2]
because the space at the beginning of the second line will get copied in. It is clear that [1 -2] is a vector of two elements. If the input had been
[1...
-2]
with no space at the beginning of the second line, then no space would be inserted as they are joined, and it would be treated as
[1-2]
but that is the single expression [(1-2)] which is the scalar -1.
In MATLAB:
  • [A-B] : the result is A minus B
  • [A- B] : the result is A minus B
  • [A -B] : the result is the array formed by taking A and adding on a column that is negative B
  • [A - B] : the result is A minus B
The original poster had
y(1) = [(a*x(1))+(b*(x(2))^2)...
-(a*(x(3))^2)+14];
and was expecting that to be treated as
y(1) = [(a*x(1))+(b*(x(2))^2)-(a*(x(3))^2)+14];
where the items are being subtracted, but because leading whitespace on the second line is preserved, it is treated as
y(1) = [(a*x(1))+(b*(x(2))^2) -(a*(x(3))^2)+14];
which is a vector of two elements.
If there had not been any space at the beginning of the second line, or if there had been a space after the - then it would have been treated as a subtraction.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by