How to iterate for loops through multiple columns within a table variable
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
darova
el 11 de Feb. de 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
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!