Filter löschen
Filter löschen

Error using for loop

3 Ansichten (letzte 30 Tage)
Mohamed Sebnat
Mohamed Sebnat am 11 Feb. 2018
i'm currently working on the discrete Wigner ville transform, wich require to use a concatenated for loops. But when i run the code i get an error message : * Subscript indices must either be real positive integers or logicals.*. a simple version of my code:
for i = 1 :length(A)
for k = 1:length(B)
a = A(i+k)*A(i-k)
end
end
  1 Kommentar
Jos (10584)
Jos (10584) am 12 Feb. 2018
It would be helpful if you give
  1. the actual code
  2. short examples of the inputs A and B,
  3. the expected output
  4. the complete error message

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Jan
Jan am 11 Feb. 2018
Posting a simplified version of the real failing code has the disadvantage, that the actual problem might be concealed.
Use the debugger to examine the problem:
dbstop if error
Now your code will stop at:
a= A(i+k) * A(i-k)
when i-k is not a positive integer. Neither 0 nor negative indices are allowed in Matlab.
  1 Kommentar
Mohamed Sebnat
Mohamed Sebnat am 12 Feb. 2018
exactly my code stop working at
a = A(i+k)*A(i-k)
i understand that 0 and negative indices are not allowed in Matlab, so i will reformulate my question: how can I work around this issue? in other word how can I write the same formulae with respect to Matlab Syntax?

Melden Sie sich an, um zu kommentieren.


possibility
possibility am 11 Feb. 2018
There are two main fails in your code. Elaborating Jan's post;
(1) when i=1 and k=1, i.e. first loop, A(i-k) = A(0) fails. The first element is starting with index 1.
(2) Even if you correct (1), you will see the same error when i=length(A), k=1. Because you attempt to an index A(i+k) which is larger than length(A).
  1 Kommentar
Mohamed Sebnat
Mohamed Sebnat am 12 Feb. 2018
how can I work around this tow issues, any ideas ?

Melden Sie sich an, um zu kommentieren.


Muhammad Usama Sharaf SAAFI
Muhammad Usama Sharaf SAAFI am 24 Jan. 2020
In first iteration , value of i and k is 1 so A(i-k) means A(0) and in matlab "0" can not be index of any array so this cause error in your case. So, i-k should not be equal to zero otherwise it will generate error.

Kategorien

Mehr zu Time-Frequency Analysis finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by