I keep getting "Array indices must be positive integers or logical values". error

1 view (last 30 days)
Heres my code, why do I get the error?
clc
clear
% Given values
L = 0.05;
qL = -100-(4.6611*5);
qR = -300+(0.3869*5);
x = linspace(0,0.05,5);
%% Requirement 4
V = zeros(length(x),1);
Vx1(0:(L/2)-1)=1;
Vx2((L/2):L) = (((qR-qL)*x.^2)/L)+((2*qL-qR)*x)-((L/4)*(qL+qR));

Accepted Answer

Walter Roberson
Walter Roberson on 22 Oct 2022
L = 0.05;
Not an integer.
Vx1(0:(L/2)-1)=1;
L/2 is 0.05/2 which is 0.025 . Subtract 1 from that to get -0.975 . So the index being requested is 0:-0.975. As the stop point is less than the start point, that results in the empty vector. It is not an error to assign a value to an empty index... it just might not be what you wanted to have happen.
Vx2((L/2):L) = (((qR-qL)*x.^2)/L)+((2*qL-qR)*x)-((L/4)*(qL+qR));
L/2 is 0.025 so (L/2)/L is 0.025:0.050 . The start value is less than or equal to the end value, so at least one element will be generated for the vector. The default increment for the : operator is 1, and 0.025+1 > 0.050 so there is no second element in the vector, just 0.025 . But 0.025 is not a positive integer, so this is an error.
What has happened is that you have confused "array" and "formula". Talking about Vx1(0:(L/2)) is an attempt to talk about a formula for a range of values: you are trying to implement something similar to a mathematical definition that has conditions attached, sometimes known as a piecewise definition ("this piece of the input space is defined as having this formula") .
MATLAB cannot handle piecewise definitions of numeric functions at all. For numeric functions, MATLAB can only talk about array indexing.
If you were using the Symbolic Toolbox, it would be possible to create a piecewise definition of a function -- but that would be done with the piecewise function that would define all of the conditions on the right hand side, with symbolic conditions not being supported for the indexing of the output name.
What you can do is use logical indexing.
L = 0.05;
qL = -100-(4.6611*5);
qR = -300+(0.3869*5);
x = linspace(0,0.05,5);
Vx1 = zeros(size(x));
Vx2 = zeros(size(x));
mask1 = x < L/2; %strict inequality
Vx1(mask1) = 1; %leaving it zeros in the other locations
mask2 = ~mask1;
Vx2(mask2) = (((qR-qL)*x(mask2).^2)/L)+((2*qL-qR)*x(mask2))-((L/4)*(qL+qR));
stairs(x, Vx1, 'k');
hold on
stairs(x, Vx2, 'b');
hold off
legend({'Vx1', 'Vx2'})
I have to wonder whether you actually want two different output arrays, or if you wanted a single output array instead.

More Answers (1)

Steven Lord
Steven Lord on 22 Oct 2022
There is no element 0 in an array in MATLAB. The first element is element number 1. You will need to change the line of code that creates the variable Vx1.
You also cannot reference or assign to an element at a fractional index. There is no element 0.05 or 0.025 in MATLAB. Therefore you will also need to change the line that creates the variable Vx2.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!