Crisp
Crisp

Reputation: 257

While loop not saving results

I am trying to do a while for a random number of occurence "y". However the while loop does not show "result" or display anything when prompted. I only get the same random numbers generated initially as my result. I want to add a second column to my result vector that shows the calculation of each X values under the conditions in the loop, for some reason the second column is not being saved. and I am just getting a column vector "X".

y = round(rand*10);

x = randi([-5,5],1,y);
X = x';
result=[X];

while isempty(result)==1
    disp('Nothing')

    if result > 2
        disp('A')
        result=[result,result+5];
    else
        disp('B')
        result=[result,result*0];
    end
end

Upvotes: 0

Views: 231

Answers (1)

rayryeng
rayryeng

Reputation: 104515

The reason why your code isn't working is due to your while condition. Specifically, take a look at the logic inside your while statement:

while isempty(result)==1

The while loop will only execute if result is empty. This is not the case, as result = X at the beginning of your code. As such, your while loop never executes.


Given your explanation in your comment below, you wish to generate a random number, and every time you do this, you want to check if the result is greater than 2 or not. If it is, add 5 to this element and add this as an element to a new column. If not, simply replace this with 0.

I would suggest you use a for loop instead of a while loop because you probably want the matrix to stop growing after a certain point.

Therefore, do something like this instead:

y = round(rand*10);
result = [];
x = randi([-5,5],1,y);

for idx = 1 : y
    if x(idx) > 2
        result = [result [x(idx); x(idx)+5]];
    else
        result = [result [x(idx); 0]];
    end
end

What you're doing first is you are determining a random number that defines the limit of how many random numbers you want to generate. We then declare an empty array that will store our result. After, we generate our random numbers, then we use a for loop to cycle through every element of x and check to see what their value is. If the value is > 2, then we add this number to our 2D matrix where its value is added by 5 to a new column. If not, we just set this value to 0.

When I did a run with this, this is what I got for result:

result =

     5     2    -5     4     5
    10     0     0     9    10

However, you can easily do this without a for loop and do it completely vectorized. You could simply replace the above code with:

y = round(rand*10);
x = randi([-5,5],1,y);

%// NEW
result = [x; zeros(1,y)];
ind = x > 2;
result(2,ind) = result(1,ind) + 5;

What the above code does is that we initialize result to be the 2D array already, where the first row consists of those numbers randomly generated, and the second row has a bunch of zeroes. We search for any randomly generated values that are > 2, then use these positions to update the second row, where we take those values that satisfy this condition and add 5 to each value. We then store the results in the second row. If you run the above code, you should be able to produce the same results.

Upvotes: 1

Related Questions