how do I make a for loop go through another for loop?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Rik
am 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.
Antworten (1)
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!