MATLAB: How to separate groups of numbers between NaN in new variables

arrayImage Processing Toolboxnew variablesseparate in new arraysvector

Hello everyone, I have a vector similar to this:
v = [NaN NaN 1 2 3 4 5 NaN NaN 6 7 8 9 10 NaN NaN NaN NaN 11 12 13 14 15 16 17 18 NaN NaN]
I would like to separate the sets of numbers into new variables. Then it would look like this:
v1 = [1 2 3 4 5]
v2 = [6 7 8 9 10]
v3 = [11 12 13 14 15 16 17 18]
I thought a lot about how to do this but I could not, I could do it manually, but it's a lot of numbers and it would be very hard work. So I decided to ask here. If anyone can help me, I will be very grateful.

Best Answer

If you have the Image Processing Toolbox, it's trivial - just one lines of code (a call to regionprops):
v = [NaN NaN 1 2 3 4 5 NaN NaN 6 7 8 9 10 NaN NaN NaN NaN 11 12 13 14 15 16 17 18 NaN NaN]
props = regionprops(~isnan(v), v, 'PixelValues')
% Now you're done. Results are in the props structure array.
% To show you , print them out:
for k = 1 : length(props)
fprintf('Values in region #%d = ', k);
fprintf('%d ', props(k).PixelValues);
fprintf('\n'); % Go to new line.
end
In the command window you'll see:
props =
3×1 struct array with fields:
PixelValues
Values in region #1 = 1 2 3 4 5
Values in region #2 = 6 7 8 9 10
Values in region #3 = 11 12 13 14 15 16 17 18