Filtering an Array for multiple different conditions
16 views (last 30 days)
Show older comments
Hello, I have an array containing values that correspond to lines from an imported text file that all contain a matching string of characters. data around those lines is the going to be pulled out and used elsewhere. I need to ensure I have at least two cronologial array values that contain correct data. I have another loop outside this section that changes an array's value to 1 if it contains the string "value = 0", so the actual TF array would have normal line numbers with some replaced with 1s(the one below is an example being used to check the for loop). I want to run a loop that checks that two sequential entries of the TF array are non-1 integers. If the current or current +1 array value is equal to 1, the current array "counter" is advanced by one or two positions depending on which conditions are met. Currently when my code runs through the TF array below I'm using for testing it does not properly update the "counter" position if the conditions are met.
The goal here is to verify that two sequential array entries meet the criteria, starting with the first array entry. If the first array value is equal to one, then the new TFpos should set to 2 and the loop should run again to check, if position 2 passes, then position 3 is checked , if position 3 is equal to 1 then the new TF pos is set to 4, as two sequentail array values must not be equal to 1. Currently the code is skipping over too many array values when I varry the TF array or not skipping when I set some array values to 1. If the loop were to function correclty, the TFpos value should end up being 6 becasue that is the first instance where two sequential array values arent equal to 1.
TF = [1; 2006; 1; 2211; 1; 5839; 6689; 6978; 7555; 7934]; % Array I am using to test functionality of for loop
TFpos = 1; % This will be the array position used once the TF array is checked
for i = 1:(TFpos + 1) % Want to start with the first cell of the array, if the first two arrays pass, no need ot check the rest of the array.
if TF(i) == 1 % If first array is equal to 1, increase TFpos by 1 and re-run the loop
TFpos = TFpos + 1;
elseif TF(i + 1) == 1 % If the first check passed, but the second array value is equal to 1, increase TFpos by 2 and re-run the loop
TFpos = TFpos + 2;
end
end
2 Comments
Jan
on 20 Jul 2022
I've read the text 5 times and cannot find out, what you want the input data are, what you want to achieve, and what the actual question is.
Please avoid commented code, because this is just confusing. Use the tools to format text and code differently to improve the readability. Please read: How to ask a good question
Answers (1)
Voss
on 20 Jul 2022
Edited: Voss
on 20 Jul 2022
I think you may have the wrong impression about what happens when you change TFpos inside the loop. Specifically, changing the value of TFpos does not automatically update the set of values that are iterated over, i.e., 1:(TFpos+1). In this case TFpos is 1 when the loop starts, so the loop will iterate over the values 1:2 only and always (the only exception being that you may exit the loop early using a break statement).
In order to have the kind of "variable-number-of-iterations" loop that you seem to intend, you need to use a while loop.
Something like this would work:
TF = [1; 2006; 1; 2211; 1; 5839; 6689; 6978; 7555; 7934]; % Array I am using to test functionality of for loop
TFpos = 1; % This will be the array position used once the TF array is checked
nTF = numel(TF);
found = false;
while TFpos < nTF
if TF(TFpos) == 1 % If first array is equal to 1, increase TFpos by 1 and re-run the loop
TFpos = TFpos + 1;
elseif TF(TFpos + 1) == 1 % If the first check passed, but the second array value is equal to 1, increase TFpos by 2 and re-run the loop
TFpos = TFpos + 2;
else
% neither TF(TFpos) nor TF(TFpos+1) is 1, so exit the while loop
found = true;
break
end
end
found
TFpos
I added the variable found so you can know whether two consecutive non-1 elements were found, because it's possible the loop could iterate over the entire TF, and not find two consecutive non-1 elements, in which case TFpos is not the location of those elements. For example:
TF = [1; 2006; 1; 2211; 1; 5839; 1; 6978; 1; 7934]; % Array I am using to test functionality of for loop
TFpos = 1; % This will be the array position used once the TF array is checked
nTF = numel(TF);
found = false;
while TFpos < nTF
if TF(TFpos) == 1 % If first array is equal to 1, increase TFpos by 1 and re-run the loop
TFpos = TFpos + 1;
elseif TF(TFpos + 1) == 1 % If the first check passed, but the second array value is equal to 1, increase TFpos by 2 and re-run the loop
TFpos = TFpos + 2;
else
% neither TF(TFpos) nor TF(TFpos+1) is 1, so exit the while loop
found = true;
break
end
end
found
TFpos
1 Comment
Voss
on 20 Jul 2022
There are other ways to do it; maybe this is the easiest:
TF = [1; 2006; 1; 2211; 1; 5839; 6689; 6978; 7555; 7934]; % Array I am using to test functionality of for loop
TFpos = findstr(TF(:).' ~= 1,[true true]);
if ~isempty(TFpos)
TFpos = TFpos(1);
end
TFpos
And on the other case, where there are not two consecutive non-1 elements:
TF = [1; 2006; 1; 2211; 1; 5839; 1; 6978; 1; 7934]; % Array I am using to test functionality of for loop
TFpos = findstr(TF(:).' ~= 1,[true true]);
if ~isempty(TFpos)
TFpos = TFpos(1);
end
TFpos
See Also
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!