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

11 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by