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

integerlogical?

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

Best Answer

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)
Related Question