필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Romendra
Romendra 2012년 1월 25일
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

채택된 답변

Romendra
Romendra 2012년 1월 31일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by