How to create a loop with unknown number of iterations?

I would like a help about a script. I have a file with three columns, each of them are numbers. I would like to satisfy a condition via a loop.I want, based on a specific equation/relationship:
mask = z_input<fin & z_input>=(fin-1);
in order to get only the elements from the file that satisfy this equation and not get an "emplty" file.
If not, then the relationship should be converted to
mask= z_input<(fin-1) & z_input>=(fin-2);
One again not then the relationship should be converted to
mask= z_input<(fin-2) & z_input>=(fin-3);
One again not then the relationship should be converted to
mask= z_input<(fin-3) & z_input>=(fin-4);
etc
I have tried the following commands:
clc
clear
filename1= 'mydata.txt';
[d1,tex]= importdata(filename1);
y_input=d1.data(:,2);
x_input=d1.data(:,1);
z_input=d1.data(:,4);
mask = z_input<fin & z_input>=(fin-1);
selected= d1.data(mask,:);
y_B=selected(:,2);
x_B=selected(:,1);
z_input_B=selected(:,4);
fin=max(z_input);
if isempty(z_input_B)
mask= z_input<(fin-1) & z_input>=(fin-2);
selected = d1.data(mask,:);
end
if isempty(z_input_B)
mask= z_input<(fin-2) & z_input>=(fin-3);
selected = d1.data(mask,:);
end
if isempty(z_input_B)
mask= z_input<(fin-3) & z_input>=(fin-4);
selected = d1.data(mask,:);
end
I need something in (i) that takes values from 0 to whatever is needed to satisfy the relation/condition and not get an "empty" file.
I mean something like
mask= z_input<(fin-i) & z_input>=(fin-(i+1));
Could you please help me?

2 Comments

In the code fin is used before it is defined.
Yes it is defined before loop

Sign in to comment.

 Accepted Answer

Jan
Jan on 3 Nov 2022
Edited: Jan on 3 Nov 2022
ready = false;
k = 1;
while ~ready
mask = z_input < fin & z_input >= (fin - k);
if any(mask)
ready = true;
else
k = k + 1;
end
end
But remember, that the value of min(z_input - fin) should help to find k directly without using a loop.
Another option is using maxk(z_input, 2) to get the 2 largest outputs, which let you determine k also.

4 Comments

Thank you, but I actually not works...
I have tried the following commands:
ready = false;
k = 0;
while ~ready
mask= z_input_B<(fin-k) & z_input>=(fin-(k+1));
if isempty(z_input_B)
ready = true;
else
k = k + 1;
end
end
but matlab crashed (I mean that do not stop executing). What I type wrong? I am absolutely sure that I have a mistake in syntax...
Could you please help me?
As long, as I do not have the values of your variables z_input_B, z_input and fin, it is hard to guess, what your code does. Most of all you have modified my code, in which I have checked, if mask is empty, while your code tests the emptiness of z_input_B, which is not changed inside the loop.
I still have tpo point out, that there is no reason to use a time-consuming loop, if the value can be found by simple arithmetics.
fin is the maximum value of the z_input variable. The point is that I would like to increase the k in the while loop with 1, until the z_input_B is not empty anymore. (according to this equation
mask= z_input_B<(fin-k) & z_input>=(fin-(k+1));, with k=0,1,.....etc)
I am uploading an example of input file in order to undersatnd what I mean
@Ivan Mich: You ignore my answer and my comments. I have mentioned, that you modify mask , but check for isempty(z_input_B).
Checking a binary mask by isempty will fail also, because this counts the number of elements without considering their values. Look in the code I've posted: any(x) does something different from isempty(mask).
Posting an example file is not useful, because I cannot guess, what I should do with this file.
My code does already, what you have asked for and you explain repeatedly, that another code does not work. Even if I mention the problem of the other code, you do not react. So I repeat another time, that a loop is a waste of time only, because maxk(z_input, 2) reveals the value of k directly: It replies the 2 largest values and you have to subtract them only.
I've posted some code, which is working as far as I can see, and mentioned a much more efficient solution. I do not see, how I could help you further. Why do you hestitate to use these information?

Sign in to comment.

More Answers (1)

What you are describing sounds like a "while" loop.
You specify a logical condition which will terminate the loop
i=0;
condition=false;
while condition == false
...
...
i=i+1;
condition = % set your termination condition
end

5 Comments

Thank you but I would like a definition.
The point is to not have an empty file (my command is if isempty(z_input_B)).
so how could i modify your code?
I tried these
i=0;
condition=isempty(z_input_B);
while condition == false
mask= z_input<(fin-i) & z_input>=(fin-(i+1));
selected = d1.data(maskA,:);
i=i+1;
condition =true % set your termination condition
end
but it is no use...
Do you have any idea in which should i modify in order to work? I have a problme to syntax it...
I don't understand what "IMM_input_B" is.
If this is the loop termination condition, it must be defined/modified inside the loop.
Based on what you are asking for, it looks like @Jan's answer is good, except you said that you wanted the index (i) to start at zero, but i'm not sure that that is really what you want. Is ther somthing about @Jan's answer that you don't like?
Thank you, but I actually not works...
I have tried the following commands:
ready = false;
k = 0;
while ~ready
mask= z_input_B<(fin-k) & z_input>=(fin-(k+1));
if isempty(z_input_B)
ready = true;
else
k = k + 1;
end
end
but matlab crashed (I mean that do not stop executing). What I type wrong? I am absolutely sure that I have a mistake in syntax...
Could you please help me??

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 3 Nov 2022

Edited:

Jan
on 5 Nov 2022

Community Treasure Hunt

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

Start Hunting!