Resetting for loop counter
8 次查看(过去 30 天)
显示 更早的评论
I'm trying to write a program to help filter data. What I would like to do is be able to import data from an excel spreadsheet, look through each column of data to determine if there are values less than 1. If so, I would like to remove the column because I don't need the data for that sensor. However, the issue I'm running into is that when I delete the column in the for loop the counter doesn't take that into account so it misses columns. For example. I know that the data set I've been running the code with should have the first column removed. Once it is removed the index value increases to 2 so it misses the original second column since that is now column 1 after the first is deleted. Is there a work around to subtract one from the counter everytime a column is deleted in the matrix?
tactarray = ManikinVestCutLeftShoulder; %creates matrix from imported TactArray pressure data
numElement = size(tactarray,2); %extracts number of sensor elements from the number of columns in dataset
%for each element it should count the number of cells where pressure is
%less than 1kPa. If numGreaterThan1 is greater 0 it should delete the column.
numElementFinal = numElement; %recalculate the total number of columns remaining after filtering data
for e = 1:numElement
A = tactarray(:,e);
numGreaterThan1 = sum(A<1);
if numGreaterThan1 > 0
tactarray(:,e) =[];
numElementFinal = numElement-1;
else
end
end
0 个评论
采纳的回答
Voss
2022-4-5
If the entire purpose of the for loop is to remove columns in tactarray that have any element less than 1, it can be replaced by this:
tactarray(:,any(tactarray < 1,1)) = [];
Or this, which is similar to your loop except the columns are not deleted until after the loop:
% set of columns to be deleted:
columns_to_delete = [];
numElement = size(tactarray,2);
for e = 1:numElement
A = tactarray(:,e);
numGreaterThan1 = sum(A<1);
if numGreaterThan1 > 0
% add e to the set of columns to be deleted:
columns_to_delete(end+1) = e;
end
end
% delete the columns:
tactarray(:,columns_to_delete) = [];
However, to achieve the method you described, you would need to use a while loop.
numElementFinal = size(tactarray,2);
e = 1;
while e <= numElementFinal
A = tactarray(:,e);
numGreaterThan1 = sum(A<1);
if numGreaterThan1 > 0
% delete column e and keep e as it is, because ...
tactarray(:,e) = [];
% ... now column e is the "next" column to check
% numElementFinal = numElement-1;
numElementFinal = numElementFinal-1;
else
% increment e by 1
e = e+1;
end
% -> check the next column (on the next iteration)
end
Or, the same thing but simplifying it a bit:
numElementFinal = size(tactarray,2);
e = 1;
while e <= numElementFinal
if any(tactarray(:,e) < 1)
% delete column e and keep e as it is, because ...
tactarray(:,e) = [];
% ... now column e is the "next" column to check
numElementFinal = numElementFinal-1;
else
% increment e by 1
e = e+1;
end
% -> check the next column (on the next iteration)
end
To answer your question, you cannot set the loop counter inside a for loop and have that value be respected on the next iteration of the loop. Here is a for loop to illustrate what happens when you try to change the loop counter (you can do it, but then it goes back to what it should be on the next iteration):
for ii = 1:5
fprintf('iteration %d',ii)
ii = 2-ii;
fprintf('set ii = %d',ii)
end
0 个评论
更多回答(1 个)
另请参阅
类别
在 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!