2 IF loops inside of a FOR loop!

Hi, I have 2 IF loops inside of a FOR loop.
for z = 1:spot_length;
if minichrom(1,z) == 0
counter = counter + 1;
if counter >= MaxH(i,j)
minichrom(1,z+1) = 1;
else continue
end
else counter(j,:) = 0;
end
After the inside "ELSE continue", the loop seems to skip to the FOR loop instead of the exterior IF and "counter" value is added up. Would you please help me?

5 Comments

without delving into code details, you have to be a bit careful with the 'continue' command. It is designed to skip to the 'for' loop. Would deleting the 'else continue' line accomplish what you want?
It's no longer like that in his updated code in the comments to me below.
Well, OK, but it sure looks like it is still there in the only code I could find below. I must be missing something.
Oh, you're right. He just split it into 2 lines now (like I recommended in my answer) so I overlooked it.
Yes, I did. But still the indices of "minichrom" and "counter" are wrong. Would you please help me correct them?

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 2 Oct 2016
Usually you don't have a semicolon on the end of the for line. And usually you don't put code on the else line. Why does counter take row and column indexes some times but at other times you treat it as a scalar instead of a 2-D matrix???
I don't know what you mean by it seems to skip to the for loop instead of doing the if. Exactly where is it when it "skips" to the for loop? And what part of the for loop? Do you know how to use the debugger? If not, see this: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/ When you know how to use that, which you WILL need to learn, then you can step line by line examining variables to see what they are and what might cause the code to skip to some part of the code or enter or not enter a "for" loop or an "if" statement. You'll need to do that because we don't know what the values of minichrome are and your explanation is ambiguous.

5 Comments

Thank you so much for your help and tips. They helped a lot. Actually I'm a beginner and I am terrible at indexes. I wonder if you could help me to find the appropriate index for 2 of my matrices(minichrom and counter) please. It would mean a lot to me.
clc
clear all
pop = 50;
nrows = 4; %the number of rows
ncolumns = 16; % number of columns
spot_length = 60; % the length of each vector(minichrom) that needs to be randomly generated as one element of the big matrix
subpop = nrows*ncolumns; %number of required random vectors
MaxH = randi([2 10],4,16); %maximum allowed number of consecutive 0s in a randomly generated vector
counter = 0; %counts the number of consecutive 0s in a vector
for i = 1:nrows
for j = 1:ncolumns
minichrom(j,:) = round(rand(1,spot_length)); % generates a 0/1 vector for each spot in the matrix
for z = 1:spot_length
if minichrom(1,z) == 0 %whenever a 0 is generated in each vector
counter = counter + 1; %counter starts to count
if counter >= MaxH(i,j) % if the maximum value is exceeded
minichrom(j,z+1) = 1; %the next element in the vector must take 1
else
continue %if not it should go to the next element in the vector(exterior IF loop)
end
else
counter = 0; %if there is a 1, counter should restart for the next 0
end
rchrom = minichrom(:)'; %I need the vectors to be placed one after another
end % I need each "rchrom" to be placed in one row(there are 4 of them) in the big matrix of (4*960)
end % I need 50(pop) of these big matrixes(4*960)
end
I have no idea if you want minichrom and counter to be functions of row, column, or spot length. What is spot length? Do you have an image of spots? Attaching a diagram or image or screenshot would be helpful. Have you learned how to step through with the debugger yet?
Sherwin
Sherwin on 2 Oct 2016
Edited: Image Analyst on 2 Oct 2016
Here in this matrix(Untitled image)
we have 4 rows (nrows=4), 2 main columns (ndolumns=2) and we have 8 (nrows*ncolumns) spots or minichroms with the length of 5(spot_length=5).
This 8 minichroms are generated randomly but in each of them the maximum allowed number of consecutive 0s is determined (MaxH) (Untitled1 image).
The code should generate random numbers of one minichrom and every time a 0 comes up the counter starts to count and compares the value of counter to the corresponding maximum allowed 0s in the MaxH matrix. If the maximum is exceeded the next element in the vector is NOT randomly generated, it should be 1, if not the generation continues. And every time a 1 comes up the counter should reset and start from 0 till the next 0 coming up. This process should be done for generating each minichroms so the a complete matrix is created. Then I need 50 of these matrices.
Thanks a lot. I am really grateful and yes I am watching the video now.
Explain how you could get 6, 7, or 8 zeros consecutively in a minichrom that is only 5 elements long? Shouldn't the values of MaxH be no bigger than 5? I mean, once all 5 elements of a minichrom are 0, then you can't add more to make 6, 7, or 8 since the minichrom vector is only 5 long. Please explain.
Sherwin
Sherwin on 2 Oct 2016
Edited: Sherwin on 2 Oct 2016
Oh I'm sorry. The image shows a small sample, the actual size of the big matrix is 4*960 in the code, and it has minichroms with length of 60, and 16 columns. I couldn't make a matrix of that size. I just wanted to show what is a minichrom or spot and the size of MaxH matrix and that the generation should be done for each minichrom.

Sign in to comment.

Categories

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

Asked:

on 2 Oct 2016

Commented:

on 3 Oct 2016

Community Treasure Hunt

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

Start Hunting!