Hi everyone! I'm working on substituing values of a matrix with the values of another matrix. So for example, if I had
A = [1 2 3 ; 2 4 5 ; 4 1 3];
V = [1 10 ; 2 20 ; 3 30 ; 4 40 ; 5 50];
I'd like to substitue the values of A that are equal to the values of the first column of V, with the value of the second column of V. For the example, change in A 1 by 10, 2 by 20 and so on. Thank you!

 Accepted Answer

Andrei Bobrov
Andrei Bobrov on 26 Feb 2017
Edited: Andrei Bobrov on 26 Feb 2017
out = reshape(V(A,2),size(A));

3 Comments

I'm getting this error: Index exceeds matrix dimensions.
OK, I solved it using just the second column of V and
out = reshape(V(A),size(A));
Thank you!
@Alyssa Webb: Without your example data this works without an error. The first column of your V seems to be the index 1:n, and then Andrei's suggestion is a very efficient look-up-table approach.

Sign in to comment.

More Answers (1)

A = [1 2 3 ; 2 4 5 ; 4 1 3];
V = [1 10 ; 2 20 ; 3 30 ; 4 40 ; 5 50];
[match, index] = ismember(A(:), V(:, 1));
A(match) = V(index, 2)

Asked:

on 26 Feb 2017

Commented:

Jan
on 26 Feb 2017

Community Treasure Hunt

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

Start Hunting!