Filter löschen
Filter löschen

how to concatenate a result of a loop to a single 2D mtrixa

1 Ansicht (letzte 30 Tage)
Romendra
Romendra am 25 Jan. 2012
Hi, I have the following code to find the linear index and the associated values of connected points in a matrix
a=[0 1 1 0 0 0
1 1 1 0 2 2
0 0 0 0 2 2
0 0 0 0 0 0]
a_connected=bwlabeln(a);
%region properties of matrix a
cc2=bwconncomp(a_connected);
stats_a=regionprops(cc2,'pixellist','pixelidxlist');
% Extracting the connected objects and their index information
% For Test Matrix
arr_test=[];
tss=[];
for k=1:numel(stats)
idx_test=stats(k).PixelIdxList;% gives linear index
Pixels_in_region_test=test_connected(idx_test);% associated val
arr_test=[idx_test,Pixels_in_region_test]
end
I want to create an output matrix that will store the result of loop but so far been unable. the matrix only stores the value from the final iteration of the loop. I have used cat but it does not work.
Any ideas on how to or what i am doing wrong?
Thanks

Akzeptierte Antwort

Romendra
Romendra am 31 Jan. 2012
okay, so the cat function does not work in this case but the vertcat sure did. Just added a little if loop in this
arr_test=[];
tss=[];
for k=1:numel(stats)
idx_test=stats(k).PixelIdxList;
Pixels_in_region_test=a_connected(idx_test);
if k==1
arr_test=[idx_test,Pixels_in_region_test]
else
tss=[idx_test,Pixels_in_region_test]
arr_test=vertcat(arr_test,tss)
end
end

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by