# Finding whether the element of the array is present in the other array and finding the index value

1 ビュー (過去 30 日間)
Ganesh Kini 2020 年 5 月 26 日

i have 2 arrays
p = -0.20000 -1.80000
v = 0.9000 0.30000 1.20000 1.50000 -1.80000
I have to compare both the array and finding the whether the element present in p is equal to the element present in v. It has to check each value ( negetive values also included )
if its a success then print the value and the index along it.
how to do it ?
i should get answer as -1.80000 and its index in both arrays

#### 2 件のコメント

Akira Agata 2020 年 5 月 26 日
I would recommend reffering ismember and/or ismembertol functions help page.
Ganesh Kini 2020 年 5 月 27 日
Thanks

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

### 回答 (1 件)

Akira Agata 2020 年 5 月 27 日

Just in case, let me post an example.
If you don't need to think about tolerance, intersect function also works.
% Example (desired output: [-1.8 1.2])
p = [-0.2 -1.8 1.2];
v = [0.9 0.3 1.2 1.5 -1.8];
% Soution1:
[idx,loc] = ismember(p,v); % or ismembertol, if assuming tolerance
out = p(idx);
idx_v = loc(idx);
idx_p = find(idx);
% Solution2:
[out,idx_p,idx_v] = intersect(p,v);

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

### Community Treasure Hunt

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

Start Hunting!

Translated by