how do I make a for loop go through another for loop?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, I need to replace values of in a table with the values of another table indicated by a 3rd table wherever it =1.
I have this example here which is proceduraly identical to a problem i have where if I can make this work its a simple step of transforming my code.
Can anyone please help?
clear
close all
x= ['B';'A';'B';'A';'B'];
y= ['B';'B';'B';'B';'B'];
z= ['C';'B';'C';'C';'B'];
C1 = cellstr(x);
C2 = cellstr(y);
C3 = cellstr(z);
Initial_Table=[C1,C2,C3];
d= ['X';'X';'X';'X';'X'];
e= ['Z';'Z';'Z';'Z';'Z'];
f= ['Y';'Y';'Y';'Y';'Y'];
C1 = cellstr(d);
C2 = cellstr(e);
C3 = cellstr(f);
Mod_Table=[C1,C2,C3];
Original_Data_txt='For_Loop_Test.csv';
Mod_Data_txt='For_Loop_Test_Rep_Val.csv';
Original_Data = xlsread(Original_Data_txt);
IDX_IND = xlsread(Mod_Data_txt);
i=1;
j=1;
for x=1:5
for i=1:3
if IDX_IND(i,j)==1
strrep(Initial_Table(i,j),Mod_Table(i,j))
i=i+1;
end
end
end
1 commentaire
Rik
le 18 Jan 2018
You are using strrep with only two inputs, and you need 3. Also, you are incrementing i inside the loop, even though that is a loop index, while j remains unchanged. As a last point: is it on purpose that you are looping over x as well?
I feel all these loops could be reduced to a single loop at the most is you use clever indexing.
Réponses (1)
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!