フィルターのクリア

Right array division with a vector

1 回表示 (過去 30 日間)
Harel Harel Shattenstein
Harel Harel Shattenstein 2018 年 3 月 25 日
編集済み: John D'Errico 2018 年 3 月 25 日
Why
1./1:1:10
return error
and
1./(1:1:10)
Does not?
Should not 1:1:10 return a vector?

採用された回答

John D'Errico
John D'Errico 2018 年 3 月 25 日
編集済み: John D'Errico 2018 年 3 月 25 日
Works for me.
1./1:1:10
ans =
1 2 3 4 5 6 7 8 9 10
What error do you think it produces?
Let me guess. it did not produce an error. It just produces something you did not expect to see.
Division is higher in the operator precedence list than colon.
So MATLAB computes 1./1, which happens to be 1. Then it generates the vector 1:1:10. Effectively, MATLAB sees this as
(1./1):1:10
When you change it by putting parens around the colon call, it does that FIRST, then divides those elements into 1.
So really, this is just a question of understanding operator precedence.
https://www.mathworks.com/help/matlab/matlab_prog/operator-precedence.html
If colon was higher in the list, then you would get the same answer with or without the parens.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by