Pattern search and string indexing

I have two cell arrays:
A = {[x(2)& x(4)& x(1)],[x(3),x(5)],[x(2),x(1)]}
B = {2465,2514,147,236,58}
where the integers in A are the indices of B items. I want to replace those indices with actual values as:
C = {[2514,236,2465],[147,58],[2514,2465]}
For loop is a bit slow. Is there an alternative?

1 件のコメント

madhan ravi
madhan ravi 2020 年 6 月 4 日
Why would you do that?

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

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 6 月 4 日

0 投票

Something like this
A = {[2 4 1],[3 5],[2 1]}; % you can put indexes as numbers
B = {2465,2514,147,236,58};
A_new = cellfun(@(x) {[B{x}]}, A)

3 件のコメント

Ronald
Ronald 2020 年 6 月 4 日
Thank you Hamza, your code has saved my day!
Ronald
Ronald 2020 年 6 月 4 日
By the way, can we set a separator in the output here?
Ameer Hamza
Ameer Hamza 2020 年 6 月 4 日
Can you explain what do you mean by separator?

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

その他の回答 (1 件)

madhan ravi
madhan ravi 2020 年 6 月 4 日

0 投票

A = @(x){[x(2), x(4), x(1)],[x(3),x(5)],[x(2),x(1)]}
B = {2465,2514,147,236,58}
C = A(B)

2 件のコメント

Ronald
Ronald 2020 年 6 月 4 日
Thanks mate! However, I found out that it couldn't slice A nicely as I wanted.
Ameer Hamza
Ameer Hamza 2020 年 6 月 5 日
FYI, following code will slice A as you want
C = A([B{:}])

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

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

質問済み:

2020 年 6 月 4 日

コメント済み:

2020 年 6 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by