MATLAB Answers

0

How do I efficiently replace data in data sets where I want to find a value in the data that is in one array and replace it with the data in the same position in another array.

Asked by Rebecka McSloy on 22 Jun 2018
Latest activity Commented on by Rebecka McSloy on 22 Jun 2018
For example: A = (N1, N2, N3..., N20) B = (NN1, NN2, NN3..., NN20) C = (Data1, Data2, .... Data13000)
N1, NN1, and Data1 are all numbers there is not a relationship between them other than I want to look in C and replace any exact values that can be found in A with the exact values found in the same position in B. I can do this in for loops but I think there must be a more efficient way than: for i = 1:length(c) for j = 1:length (A) if C(i) == A(j) C(i) = B(j) end end end

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Steven Yeh on 22 Jun 2018
 Accepted Answer

You can use a single loop with find function:
a = 1:10;
b = 11:20;
c = 1:2:10;
for i = 1:length(c)
index = find(c(i) == a);
c(i) = b(index);
end