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 回表示 (過去 30 日間)
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

採用された回答

Steven Yeh
Steven Yeh 2018 年 6 月 22 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by