Please explain the following line of code

조회 수: 1 (최근 30일)
Adnaan
Adnaan 2019년 9월 16일
댓글: Adnaan 2019년 9월 17일
Please explain the following line of code:
o(2.*[1:floor(D/2)]-1)=-32
where
o=-30+60*rand(1,D);

채택된 답변

Guillaume
Guillaume 2019년 9월 16일
What couldn't you run the code and see what it does?
Your line of code is just a convoluted of doing:
o(1:2:end) = -32
which basically puts 32 in all the odd indices of o.
  댓글 수: 3
Guillaume
Guillaume 2019년 9월 16일
There's nothing special about it. D is the length of o. The line creates the vector 1:floor(D/2) which is thus 1:floor(end/2) with respect to o. It them multiplies that vector by 2, so it makes the vector 2, 4, 6, ..., floor(end/2)*2 == end (or end-1). It then subtract 1 from that, so you get 1, 3, 5, ... end. That vector is then used to index o. Indexed values are assigned -32.
As I said, it just an inefficient and obscure way of doing:
o(1:2:end) = -32;
Don't use that original code. There is nothing useful about it.
Adnaan
Adnaan 2019년 9월 17일
Thank you very much indeed. I am trying to learn language. So, get stuck some times at such obscure codes. I am grate ful for your help.

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

추가 답변 (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