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 件のコメント

John D'Errico
John D'Errico 2015 年 4 月 28 日
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.

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2015 年 4 月 27 日
編集済み: Image Analyst 2015 年 4 月 27 日

1 投票

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 件のコメント

nmartz
nmartz 2015 年 4 月 27 日
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()??
Image Analyst
Image Analyst 2015 年 4 月 28 日
[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.
nmartz
nmartz 2015 年 4 月 28 日
Now it hits in the loop for...end with error message:Cell contents assignment to a non-cell array object. :(
Image Analyst
Image Analyst 2015 年 4 月 28 日
編集済み: Image Analyst 2015 年 4 月 28 日
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.
nmartz
nmartz 2015 年 4 月 28 日
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.
Image Analyst
Image Analyst 2015 年 4 月 28 日
Sorry - use PixelValues instead of PixelList.
nmartz
nmartz 2015 年 4 月 28 日
編集済み: nmartz 2015 年 4 月 28 日
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{:});
Image Analyst
Image Analyst 2015 年 4 月 28 日
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
nmartz
nmartz 2015 年 4 月 28 日
OK we did it, for one more time Image Analyst you are amazing!! Thanks a lot!!

サインインしてコメントする。

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2015 年 4 月 27 日
編集済み: Andrei Bobrov 2015 年 4 月 27 日

0 投票

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 件のコメント

nmartz
nmartz 2015 年 4 月 27 日
Thanks for your response, I checked this but I had this output: Error using horzcat Dimensions of matrices being concatenated are not consistent.

サインインしてコメントする。

カテゴリ

質問済み:

2015 年 4 月 27 日

コメント済み:

2015 年 4 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by