Sort out square numbers

조회 수: 13 (최근 30일)
Ambro Chunky
Ambro Chunky 2020년 12월 9일
댓글: Walter Roberson 2021년 10월 28일
(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
Kakande  Hannington
Kakande Hannington 2021년 10월 28일
We need serious help on this one as it's not easy
Walter Roberson
Walter Roberson 2021년 10월 28일
Hint:
sqrt(65)
ans = 8.0623
take the 8 part if it
8
ans = 8
square that
8^2
ans = 64
does that equal the original number 65?
What about 121?
sqrt(121)
ans = 11
take the 11 part of it
11
ans = 11
square that
11^2
ans = 121
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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by