필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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

채택된 답변

Chad Greene
Chad Greene 2015년 4월 13일
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
Chad Greene
Chad Greene 2015년 4월 13일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by