How to iterate for loops through multiple columns within a table variable

12 views (last 30 days)
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!

Answers (1)

darova
darova on 11 Feb 2021
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!