Comparing elements in a vector

1 回表示 (過去 30 日間)
Joel Bovin
Joel Bovin 2020 年 11 月 10 日
コメント済み: per isakson 2020 年 11 月 11 日
Hello!
I need to check if a squared element exists in the vector as another element.
for example x=[2 3 4]
so the squared value of 2 would be 4 which is in the vector and would result in a returnation of y=true.
This vector is quite small and I need a solution to work for bigger vectors but you get the idea of what I need.
I would love to find some documentation on this if you have it available, been searching for hours without finding anything that I can make work. I tried using find() among other things but couldn't make it work.
  8 件のコメント
David Hill
David Hill 2020 年 11 月 10 日
Misread that one. How about:
logical(nnz(ismember(x,x.^2)));
David Goodmanson
David Goodmanson 2020 年 11 月 11 日
編集済み: David Goodmanson 2020 年 11 月 11 日
any(x' == x.^2, 'all')
uses more memory than most techniques but still for a vector of length 1000 it's just 8 MBytes

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

採用された回答

per isakson
per isakson 2020 年 11 月 11 日
編集済み: per isakson 2020 年 11 月 11 日
"My idea is to start of squaring the elements in x and then to check if any element in x equals any element in sqr." That's a good start.
Are all elements of the vector, x, whole numbers?
Steven hinted about ismember. Thus, look it up in the documentation. Type ismember in the command window, select it by double-clicking, right-click and chose "Help on Selection". Reading documentation is a large part of using Matlab.
"any element in x equals any element in sqr" That's what ismember does.
%%
x = 2:12;
sqr = x.^2;
%
[ism,loc] = ismember( sqr, x );
if any( ism )
x(loc(ism))
else
disp('None found')
end
%%
In the bottom of the documetation page of ismember you'll find See Also: ... intersect ... . It's documentation says: "C = intersect(A,B) returns the data common to both A and B, " Worth trying.
intersect( sqr, x )
  2 件のコメント
Joel Bovin
Joel Bovin 2020 年 11 月 11 日
I read about ismembet but ran into trouble when it returned a logical array and I didn't know how to handle the logical values in it. So it worked for me but got stuck on how to extrapolate the values from the array, though I guess I only need to check if there is a 1 in it.
I've got several answers that all look good so I'm accepting the answer
Thank you all for the help!
per isakson
per isakson 2020 年 11 月 11 日
"but ran into trouble when it returned a logical array" The logical array is true for those elements of sqr, which are members of x. Thus
>> sqr(ism)
ans =
4 9
>>
The best way of indexing isn't always the first that comes to mind, which I unwittingly illustrated in my answer.
However, I think intersect is the best alternative. The name makes sense if one knows a little about set theory.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by