How to iterate for loops through multiple columns within a table variable
12 次查看(过去 30 天)
显示 更早的评论
Hello,
I would like the following piece of code to iterate through all columns of the table variable "datatest" instead of looping onlu on the first column. How would I change this code to apply to several columns?
The code I am using right now works great. What it does is going throrugh the column and changing a "1" to a "0" if the three rows preceding the "1" are all "0".
[datatest,Txt]=xlsread('/Users/Documents/MATLAB/data_test_2.xlsx');
excel_file_final(:,:)=datatest(:,:);
for i=1:length(datatest)
if datatest(i,1)==1
if datatest(i-4,1)==0 && datatest(i-3,1)==0 && datatest(i-2,1)==0 && datatest(i-1,1)==0
excel_file_final(i,1)=0;
else
excel_file_final(i,1)=datatest(i,1);
end
end
end
Thank you very much!
0 个评论
回答(1 个)
darova
2021-2-11
Add one more for loop
[datatest,Txt]=xlsread('/Users/Documents/MATLAB/data_test_2.xlsx');
excel_file_final(:,:)=datatest(:,:);
for i = 5:size(datatest,1) % number of rows
for j = 1:size(dataset,2) % number of columns
if datatest(i,j)==1 && all(datatest(i-4:i-1,j)==0)
excel_file_final(i,j)=0;
else
excel_file_final(i,j)=datatest(i,j);
end
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!