this is John BG firstname.lastname@example.org
I have done the following corrections to your code
1.- the mismatch error was cause by the i=i+1 in the while loop attempting to index +1 beyond the length of the vector.
a for loops suffices.
2.- there's no need for the variable row_select
3.- there's no need for the find producing row_select
4.- return 0, not 1, in QQ, for all those single values of Q that do not imply random selection.
Setting those to 1 may be misleading, because 1 is a possible index for a sub-selection within the partial ranges.
Q = [7 7 7 8 8 8 10 18 27 42 65 49 54 65 78 78 78 82 87 98 98]; B = unique(Q) Ncount = histc(Q, B) i = 1; for i=1:1:length(B) % < length(Q) QQ(i) = 0 % Q(i); % or QQ(i) = 1, but 1 may be index of random selection, thus potentially confusing if Ncount(i) > 1 % [row col] = find(Q == B(i)); % no need for variable row_select QQ(i) = randsample(Ncount(i),1); end % i = i + 1; end
If you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
1.- as usual, and as a compliment, Jan Simon is on the top of his game always providing extremely useful insight and solutions to all questions he contributes to,
but it my opinion for this particular answer there's no need for any additional functions like runlength.m
many people do not even have a complier installed, which fills up the screen with all the checks and the suggestion to install one, or to download something from a website. Again, I find RunLength a powerful function, but in the context of generating random selection of sub-sections, there's no need for such advanced function.