Create columns with no zero sequentially elements of a vector

I have a vector A=[4,2 3 2 2 0 0 1 2 2 0 0 1 2 3] and I am trying to create columns in new matrix with no zero sequentially elements, with a loop without success.Β1=[4,2 3 2 2] B2=[1 2 2] etc
I want to have a column with elements of A and when I find zero to create another column. Do you believe that it is feasible?

1 comentario

You do realize that matrices MUST be rectangular? So all columns of a matrix must be the same length. This is why you failed to produce a result, since your result would not have that property.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 27 de Abr. de 2015
Editada: Image Analyst el 27 de Abr. de 2015
This is very easy with the Image Processing Toolbox. Simply use bwlabel() (and regionprops() if you're going to have some unknown number of regions rather than exactly 3).
A=[4 2 3 2 2 0 0 1 2 2 0 0 1 2 3]
labeled = bwlabel(A)
B1 = A(labeled==1)
B2 = A(labeled==2)
B3 = A(labeled==3)
Do you have that toolbox?

9 comentarios

Thanks a lot it works but I don't understand what can I do in case of have unknown number of regions. With bwlabel it must do this defining B1...B183, but how can I solve this with regionprops()??
[labeled, numRegions] = bwlabel(A);
measurements = regionprops(labeled, 'PixelList');
for k = 1 : numRegions
B{k} = measurements(k).PixelList;
end
B will now be a cell array and each element (cell) of B will contain an array with however many numbers are in that region: 5, 3, 3, etc. - whatever it happens to be.
Now it hits in the loop for...end with error message:Cell contents assignment to a non-cell array object. :(
Image Analyst
Image Analyst el 28 de Abr. de 2015
Editada: Image Analyst el 28 de Abr. de 2015
What is your code? This works just fine. I actually just tried it.
A=[4 2 3 2 2 0 0 1 2 2 0 0 1 2 3]
labeled = bwlabel(A)
[labeled, numRegions] = bwlabel(A);
measurements = regionprops(labeled, A, 'PixelList');
for k = 1 : numRegions
B{k} = measurements(k).PixelList
end
You've done something different than what I told you to do because I just copied and pasted what was here and it worked perfectly.
Its OK with error I just forgot the magic 'clear all'. I have also copied your code but about your result the cell B now has actually the position of elements B{1,1}=[1,1 ; 2,1 ; 3,1 ; 4,1; 5,1] and not the elements. On the other hand I would like to have [4 2 3 2 2] the next cell to be [1 2 2] and the next [1 2 3] instead.
Sorry - use PixelValues instead of PixelList.
nmartz
nmartz el 28 de Abr. de 2015
Editada: nmartz el 28 de Abr. de 2015
Its a nightmare!!! Image Analyst I am really sorry for all the questions.
clear all;
A=[4 2 3 2 2 0 0 1 2 2 0 0 1 2 3];
labeled = bwlabel(A);
[labeled, numRegions] = bwlabel(A);
measurements = regionprops(labeled, 'PixelValues');
for k = 1 : numRegions;
B{k} = measurements(k).PixelValues;
end;
Error using regionprops>getPropsFromInput (line 1171)
REGIONPROPS needs I as an input to calculate 'PixelValues'.
Error in regionprops>ParseInputs (line 1133)
reqStats = getPropsFromInput(startIdxForProp, ...
Error in regionprops (line 154)
[I,requestedStats,officialStats] = ParseInputs(imageSize,
varargin{:});
For regionprops() to get the values of the original array, you need to supply it.
A=[4 2 3 2 2 0 0 1 2 2 0 0 1 2 3];
[labeled, numRegions] = bwlabel(A);
measurements = regionprops(labeled, A, 'PixelValues');
for k = 1 : numRegions
B{k} = measurements(k).PixelValues
end
See my Image Segmentation Tutorial for a more comprehensive and well commmented demo. http://www.mathworks.com/matlabcentral/fileexchange/25157-image-segmentation-tutorial
OK we did it, for one more time Image Analyst you are amazing!! Thanks a lot!!

Iniciar sesión para comentar.

Más respuestas (1)

Andrei Bobrov
Andrei Bobrov el 27 de Abr. de 2015
Editada: Andrei Bobrov el 27 de Abr. de 2015
B = zeros(size(A));
t = A > 0;
B(strfind(0,t],[0,1])) = 1;
b0 = cumsum(B);
Bout = accumarray(b0(t)',A(t)',[],@(x){x'});

1 comentario

Thanks for your response, I checked this but I had this output: Error using horzcat Dimensions of matrices being concatenated are not consistent.

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 27 de Abr. de 2015

Comentada:

el 28 de Abr. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by