フィルターのクリア

How to return values mapped to a variable?

2 ビュー (過去 30 日間)
ekagra gupta
ekagra gupta 2022 年 6 月 25 日
回答済み: Soufian MELLAHI 2022 年 6 月 25 日
Hello,
I have two vectors
a = [1 2 3 4 5 6 7 8 9 10]
b = [4 5 12 45 21 52 69 10 22 0]
and
a_temp = isprime(a)
How can I get values of "b" corresponding to "a_temp"?
  1 件のコメント
Soufian MELLAHI
Soufian MELLAHI 2022 年 6 月 25 日
編集済み: Soufian MELLAHI 2022 年 6 月 25 日
Hello, (I modify my answer) you can just search/find indexes based on values of a_prime by using the following indexation method :
a_prime_val=a(a_prime); c=b(b==a_prime_val(:)); % you will get a vector of the occurences of a_prime_val within b.
i hope it answers your questions.

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

採用された回答

Soufian MELLAHI
Soufian MELLAHI 2022 年 6 月 25 日
Hello, (I modify my answer) you can just search/find indexes based on values of a_prime by using the following indexation method : a_prime_val=a(a_prime); c=b(b==a_prime_val(:)); % you will get a vector of the occurences of a_prime_val within b. i hope it answers your questions.

その他の回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2022 年 6 月 25 日
編集済み: Dyuman Joshi 2022 年 6 月 25 日
Not sure what you want exaclty
a = [1 2 3 4 5 6 7 8 9 10];
b = [4 5 12 45 21 52 69 10 22 0];
a_temp = isprime(a)
a_temp = 1×10 logical array
0 1 1 0 1 0 1 0 0 0
b_temp = b(a_temp)
b_temp = 1×4
5 12 21 69
  2 件のコメント
Soufian MELLAHI
Soufian MELLAHI 2022 年 6 月 25 日
Your answer provides values in b bases on prime numbers indexes in a but no equivalent with occurences of the same prime numbers in b. It depends on the real need. If he needs the prime numbers as same as present in b which is more logical. we need to remap the indexes as i have done in my answer. Otherwise your answer seems fitting.
Image Analyst
Image Analyst 2022 年 6 月 25 日
@Soufian MELLAHI he gave him exactly what he asked for. But we all know that what a poster asks for is not always what they mean. If it's not what he intended, then perhaps the original poster can clarify what's needed. And say if it's homework or not.
By the way, can you transfer your Answer from the above comment section to down here in the official Answer section so you might get credit for it if he accepts your answer? Thanks in advance. 🙂

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

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by