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.

1 view (last 30 days)
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

Accepted Answer

Steven Yeh
Steven Yeh on 22 Jun 2018
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

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!