Sort out square numbers
12 ビュー (過去 30 日間)
古いコメントを表示
(Homework) Create an array of N numbers. Now find a single MATLAB statement that picks out from that array the 1,4,9,16,…,√Nth entries, i.e. those numbers which have indices that are square numbers.
[SL: formatted text as text not code]
3 件のコメント
Walter Roberson
2021 年 10 月 28 日
Hint:
sqrt(65)
take the 8 part if it
8
square that
8^2
does that equal the original number 65?
What about 121?
sqrt(121)
take the 11 part of it
11
square that
11^2
does that equal the original number 121?
By using this logic, if you had the index of a value in a vector, you could detect whether the index is the square of an integer. Comparisons ("does that equal") can return a logical vector, which you can then use to index the original array.
(Not saying this is the easiest way!)
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!