Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

index must be a positive integer or logical

조회 수: 1 (최근 30일)
ihab
ihab 2015년 10월 2일
마감: MATLAB Answer Bot 2021년 8월 20일

this is part of my code with SPEED=8 ASPECT=30

DOPPLER = 2925/(2925 + SPEED*cos(ASPECT));
TONE_A=88*DOPPLER;
atten(TONE_A)=( 0.1*TONE_A^2/(1+TONE_A^2))+(40*TONE_A^2/(4.100+TONE_A^2))+(2.75*(10^-4)*TONE_A^2)+0.003;

i get Attempted to access atten(87.9768); index must be a positive integer or logical.

what is the problem ?

  댓글 수: 1
Varun Pai
Varun Pai 2015년 10월 14일
From the above code, i understand that you are assigning the TONE_A th position element of matrix 'atten'. Matrix indexing in matlab can only be a positive integer or logical.
eg: atten(1),atten(2)..etc

답변 (1개)

Star Strider
Star Strider 2015년 10월 2일

You first need to define ‘atten’ as a function if you want to call it as one:

atten = @(TONE_A) ( 0.1*TONE_A^2./(1+TONE_A^2))+(40*TONE_A^2./(4.100+TONE_A^2))+(2.75*(10^-4)*TONE_A^2)+0.003;  % Anonymous Function ‘atten’
atten_TONE_A = atten(TONE_A);       % Call ‘atten’ & Assign Output To A Variable
  댓글 수: 3
Walter Roberson
Walter Roberson 2015년 10월 14일
atten = @(TONE) ( 0.1 * TONE^2 ./ (1+TONE.^2)) + (40 * TONE.^2 ./ (4.100 + TONE.^2)) + (2.75 * (10^(-4)) * TONE.^2) + 0.003;  % Anonymous Function ‘atten’
atten_TONE_A = atten(TONE_A);
atten_TONE_B = atten(TONE_B);
Thorsten
Thorsten 2015년 10월 14일
편집: Thorsten 2015년 10월 14일

No. You define a single function for a TONE

 atten = atten = @(TONE) ( 0.1*TONE.^2./(1+TONE.^2))+(40*TONE.^2./(4.100+TONE.^2))+(2.75*(10^-4)*TONE.^2)+0.003; % Anonymous Function ‘atten’

And call it with different arguments

 TONE_A = 88*DOPPLER;
 AA = atten(TONE_A);
 TONE_B = 123*DOPPLER: % or whatever TONE_B you have
 AB = atten(TONE_B);

If you have many TONEs, this scheme will be cumbersome and you can call atten with a vector of all your TONEs

 A = atten([TONE_A TONE_B TONE_C])

Note that I have changed Star Strider's function to use point-wise operations .^ such that it can handle multiple inputs.

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by