フィルターのクリア

How to match two arrays in Matlab

17 ビュー (過去 30 日間)
Yro
Yro 2020 年 3 月 19 日
コメント済み: Yro 2020 年 3 月 20 日
Hi, I want to know if it is possible to match the values of an array (cell by cell) A (:, 1) with an array B (:, 2) so that when calling any array C (:, 1) it gets the values of B (:, 2). Thanks in advance.
A = B = input C =
1 21 22 1
2 10 11 3
3 23 25 6
4 33 30 8
5 32 35 7
6 21 20 5
7 25 27 4
8 24 18 9
9 19 16 10
10 10 33 2
  7 件のコメント
BobH
BobH 2020 年 3 月 19 日
C seems to a selector array. The value in C is an index into A and B -?
Yro
Yro 2020 年 3 月 20 日
編集済み: Yro 2020 年 3 月 20 日
Hi, thanks for your attention, the C array are random values to read from a file, what I need is, once read, get the values corresponding to B. Something like this:
1 = [21 22]
3 = [23 25]
6 = [21 20]
...

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

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 3 月 20 日
You can do this type of indexing as follows
A = (1:10)';
B = [21 22;
10 11;
23 25;
33 30;
32 35;
21 20;
25 27;
24 18;
19 16;
10 33];
C = [1 3 6 8 7 5 4 9 10 2]';
D = B(C,:)
result
D =
21 22
23 25
21 20
24 18
25 27
32 35
33 30
19 16
10 33
10 11
  2 件のコメント
BobH
BobH 2020 年 3 月 20 日
The original question relied on values in A to locate the proper row in B. If A might be something other than 1-10 then it must be part of the solution. If A is merely a row index for B then there is no need for A.
Yro
Yro 2020 年 3 月 20 日
Thank you very much, it works. Thank you all for your time and attention. Really very grateful.

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

その他の回答 (1 件)

BobH
BobH 2020 年 3 月 19 日
編集済み: BobH 2020 年 3 月 19 日
Is this what you are trying to get? Second column of B based on C matching A
A = 1:10;
B = [21 22;10 11;23 25;33 30;32 35;21 20;25 27;24 18;19 16;10 33];
C = [1 3 6 8 7 5 4 9 10 2];
arrayfun(@(X) B( A(X), 2 ), C )
ans =
22 25 20 18 27 35 30 16 33 11
  2 件のコメント
Yro
Yro 2020 年 3 月 20 日
Thanks for your attention, something like that, but I really need, as I described earlier, something like this for the values of array:
1 = [21 22]
3 = [23 25]
6 = [21 20]
...
BobH
BobH 2020 年 3 月 20 日
Please update the original question to reflect your desired result. As it stands now, you are only asking for the second column of B "...it gets the values of B (:, 2)"

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by