Subscript indices must either be real positive integers or logicals - quick question on how to fix

1 view (last 30 days)
Greetings all,
I have the following code:
Response_values_H= 0:0.01:9;
R_h=zeros(size(Response_values_H));
for n=1:length(Response_values_H)
rValue_H = Response_values_H(n);
if rValue_H < 9.1
R_h(n)=sqrt(4*rValue_H.^2+1)-2*rValue_H;
else
R_h(n)=0;
end
end
I have a quick question. When I enter an integer, i.e. R_h(25), it returns output. But when I try and enter a decimal, say R_h(2.5), I get: Subscript indices must either be real positive integers or logicals.
So, I tried using round(), but I want to input a decimal. This may be a simple solution, but how can I input a decimal without getting an error? Is there any way to fix it knowing the input must be a decimal?
Thanks! -J

Accepted Answer

Chad Greene
Chad Greene on 13 Apr 2015
As I understand your question, you run the code above, then you want to find out values of R_h for some given x value. When you type R_h(25), it'll give you the 25th value of R_h. If you want to find some value of R_h corresponding to an arbitrary value of Response_values, you'll need to interpolate:
Response_values_H= 0:0.01:9;
R_h=zeros(size(Response_values_H));
for n=1:length(Response_values_H)
rValue_H = Response_values_H(n);
if rValue_H < 9.1
R_h(n)=sqrt(4*rValue_H.^2+1)-2*rValue_H;
else
R_h(n)=0;
end
end
plot(Response_values_H,R_h);
xlabel('Response values')
ylabel('R_h')
R_h_i = interp1(Response_values_H,R_h,2.5);
hold on
plot(2.5,R_h_i,'ro','markersize',10)
  2 Comments
Chad Greene
Chad Greene on 13 Apr 2015
By the way, this is a faster and cleaner method of calculating R_h:
Response_values_H= 0:0.01:9;
R_h = sqrt(4*Response_values_H.^2+1)-2*Response_values_H;
R_h(Response_values_H>=9.1) = 0;
No need for preallocating, looping, or if statements.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!