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

조회 수: 12 (최근 30일)
Giovanna Del Sordo
Giovanna Del Sordo 2021년 2월 4일
답변: darova 2021년 2월 11일
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!

답변 (1개)

darova
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by