# Pattern search and string indexing

1 view (last 30 days)
Ronald on 4 Jun 2020
Commented: Ameer Hamza on 5 Jun 2020
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?
madhan ravi on 4 Jun 2020
Why would you do that?

Ameer Hamza on 4 Jun 2020
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 CommentsShow 1 older commentHide 1 older comment
Ronald on 4 Jun 2020
By the way, can we set a separator in the output here?
Ameer Hamza on 4 Jun 2020
Can you explain what do you mean by separator?

madhan ravi on 4 Jun 2020
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)
Ronald on 4 Jun 2020
Thanks mate! However, I found out that it couldn't slice A nicely as I wanted.
Ameer Hamza on 5 Jun 2020
FYI, following code will slice A as you want
C = A([B{:}])