Find the position of numbers in array

8 ビュー (過去 30 日間)
Marco Piazza
Marco Piazza 2022 年 9 月 30 日
編集済み: Ergin Sezgin 2022 年 9 月 30 日
Hello everybody, I'm trying to find the position of some values in an array. The values i'm interested in are in a 1x182 vector and I want to find they're position in a 1x1820 vector (practically I'm just looking for a percentile). Can you guess how I can find them?
PS: i did it with a for loop using the find function but i think it is a non-efficient way, there must be an easier and fast way!
Thank a lot in advance,
Marco

回答 (3 件)

Walter Roberson
Walter Roberson 2022 年 9 月 30 日
percent_present = mean(ismember(SmallVector, LargeVector)) * 100

Star Strider
Star Strider 2022 年 9 月 30 日
I am not certain what you are asking.
Perhaps using the ismember (or ismembertol) function will do what you want.
  2 件のコメント
Marco Piazza
Marco Piazza 2022 年 9 月 30 日
Thnaks for answering, I'll try to explain it better with an example
a = [4 5 7 2 1 6 3 9 8]
b = [2 7 8]
Can MATLAB find the position of each numbers in vector b in vector a? So the answer I need is
ans = [4 3 9]
Star Strider
Star Strider 2022 年 9 月 30 日
Getting them is straightforward. Getting them in that exact order requires a bit of coding gymnastics —
a = [4 5 7 2 1 6 3 9 8];
b = [2 7 8];
[~,idx] = ismember(a, b);
[idx_sorted,sortidx] = sort(idx(idx>0));
idx2 = find(idx);
Result = idx2(sortidx)
Result = 1×3
4 3 9
.

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


Ergin Sezgin
Ergin Sezgin 2022 年 9 月 30 日
編集済み: Ergin Sezgin 2022 年 9 月 30 日
Hello Marco,
Following code might help with your task:
rng(2022)
inputArray = 100*rand(1,1820); % you should use your array
selectedElems = randperm(1820,182); % random indexing for subset,
yourVector = inputArray(selectedElems); % you should use your vector instead
elemPositions = zeros(size(yourVector)); % result vector for determined positions
for i=1:size(yourVector,2)
elemPositions(1,i) = find(inputArray==yourVector(1,i));
end
isequal(selectedElems, elemPositions) % Compare real and determined positions
ans = logical
1
Good luck

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by