M
M
mphys2021-12-29 19:44:13
MATLAB
mphys, 2021-12-29 19:44:13

How to understand an expression in MATLAB?

There is such a construction in matlab, I took it from the help:

n = -5:1:5;
x = dirac(n);
idx = x == inf; 
x(idx) = 1;

The construction considers the Dirac delta function and replaces infinity with one in it so that the function can be visualized through plot.

How to understand the penultimate line? In what order does the compiler execute it?
I have not seen a similar construction anywhere in the "basics of working with matlab", although it is clear from the meaning of the code that the index of an element equal to inf is stored in idx. Maybe there is a help page on the account of such feints with ears? (I have not met)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mphys, 2021-12-29
@mphys

tldr
Так, ну начинаю разбирать сам.
Можно переписать так:
idx = (x == inf);
Это тоже работает. Если выполнить отдельно выражение в скобках:
>> x == inf
ans =
  1×11 logical array
   0   0   0   0   0   1   0   0   0   0   0

то оно работает логично, сравнивает каждый элемент с Inf и выдает логическое да/нет.
Теперь вопрос сводится к тому, почему
x(idx) = 1;
работает именно так, как оно работает, первый раз вижу чтобы аргумент матрицы в круглых скобках был логическим массивом, обычно там просто порядковый номер элемента (или группы элементов).

Found the manual:
https://www.mathworks.com/help/matlab/math/array-i...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question