Find the element that is a sqrt of another element in the same array function?

1 回表示 (過去 30 日間)
Khadijat Chagayeva
Khadijat Chagayeva 2020 年 10 月 8 日
コメント済み: Rik 2020 年 10 月 8 日
Hi, my task is to create a function that looks through an array of numbers in a vector and checks if any of the elements are square roots of each other. Ex x= [ 2 3 4], in this case my function needs to return statement true, since 2 is the squareroot of 4. However, if x =[ 2 3 5] then the function should return the statement false since none of the numbers 2,3,5 are squareroots of each other. I've coded it this way so far but with no success
function y = isItSquared(x)
x1=x.^2;
for i = 1:length(x)
if x(i) == x1(i)
y = true;
elseif x(i) ~= x1(i)
y=false;
end
end
  2 件のコメント
Steven Lord
Steven Lord 2020 年 10 月 8 日
However, if x =[ 2 3 1] then the function should return the statement false since none of the numbers 2,3,1 are squareroots of each other.
The square root of 1 is 1 so shouldn't that example return true?
Khadijat Chagayeva
Khadijat Chagayeva 2020 年 10 月 8 日
oh yeah, you're righ. I meant like [2 3 5], then the satement shhould be false

サインインしてコメントする。

回答 (2 件)

Bjorn Gustavsson
Bjorn Gustavsson 2020 年 10 月 8 日
Hint: Which number are you actually comparing, and which number-combinations do you need to compare?
One thing I'll suggest is that you display all the intermediate results during the processing - this helps understanding
what your functions does (this is unfortunately not always the same as you want it to do. May have wailed against this experience, to no avail.) You could include a line something like this in your loop just after the for-line:
disp([i x(i), x1(i), x(i) == x1(i)])
That will show you what's going on. This will obviously be an absolute horror for large input-arrays, but suitable for initial development when you test with small arrays.
HTH
  2 件のコメント
Khadijat Chagayeva
Khadijat Chagayeva 2020 年 10 月 8 日
I get what the function is doing, but i still have no idea how i'm supposed to make it return false or true and how to code for it to check whether the elements in the same array are squareroots of each other
Bjorn Gustavsson
Bjorn Gustavsson 2020 年 10 月 8 日
Clearly you dont get what the function is doing well enough. Have you tried to run your function with my disp-line added and if so have you thought about what the output does? What would you need to do to modify your code?

サインインしてコメントする。


Rik
Rik 2020 年 10 月 8 日
To compare many elements to each other I would recommend the ismember function.
  4 件のコメント
Khadijat Chagayeva
Khadijat Chagayeva 2020 年 10 月 8 日
yeah because i don't know any other way to find the element. I don't know how to find the squareroot of an element in the same vector, so i created a copy of it but ^2
Rik
Rik 2020 年 10 月 8 日
That is an excellent strategy: now you have two vectors, one of which is the square root of the other. That means that if any of them is a member of the other, you must have a root.
Consider your example:
x= [ 2 3 4]
x1=[4 9 16]
Because one of the values in the second vector occurs in the first, you have to return true.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by