The values of an array match the index of elements in a vector. For example, the value of 5 in array1 below corresponds to whatever is in index 5 in vector1, i.e. the number 3 in this case. So how do I replace the value of the original number in array1 in the example by the number at the corresponding index in vector1? I realize this is a little confusing, so here's a mini example of what I want to do:
array1 = [1 2; 4 2; 5 5; 4 2];
vector1 = [2 1 1 4 3 4];
result = [2 1; 4 1; 3 3; 4 1];

 採用された回答

Paul
Paul 2022 年 7 月 17 日

0 投票

array1 = [1 2; 4 2; 5 5; 4 2];
vector1 = [2 1 1 4 3 4];
result = [2 1; 4 1; 3 3; 4 1]
result = 4×2
2 1 4 1 3 3 4 1
result1 = vector1(array1)
result1 = 4×2
2 1 4 1 3 3 4 1

3 件のコメント

L'O.G.
L'O.G. 2022 年 7 月 17 日
@Paul Wow, I didn't know you could do that. Very cool! Could you explain how that works? I don't get it, but I'd like to understand if you don't mind.
Paul
Paul 2022 年 7 月 17 日
vector1(array1) treats each entry of array1 as the linear index into vector1. The output takes the same shape as array1. It's the same as
array1 = [1 2; 4 2; 5 5; 4 2];
vector1 = [2 1 1 4 3 4];
reshape(vector1(array1(:)),size(array1))
ans = 4×2
2 1 4 1 3 3 4 1
L'O.G.
L'O.G. 2022 年 7 月 17 日
Thank you. That is really neat!

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

その他の回答 (1 件)

DGM
DGM 2022 年 7 月 16 日

0 投票

Your example isn't consistent. I'm assuming you want this:
A = [10 20; 30 40; 50 60];
idx = [2 1; 1 4; 3 3];
B = A.';
B = B(idx)
B = 3×2
20 10 10 40 30 30

1 件のコメント

L'O.G.
L'O.G. 2022 年 7 月 16 日
Thank you, but no, that's not what I want. I've edited my question to hopefully make it clearer.

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2021b

質問済み:

2022 年 7 月 16 日

コメント済み:

2022 年 7 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by