how do I make a for loop go through another for loop?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Rik
2018-1-18
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.
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!