How can i arrange the coordinates from a matrix ?
1 回表示 (過去 30 日間)
古いコメントを表示
Hi...... I have a matrix with ones and zeros. For reference its given below X=[0 0 0 1 0;0 0 0 1 0;0 1 1 1 0;0 1 0 0 0;0 1 0 0 0] and i have to get the coordinates in the given format. (5,2)(4,2)(3,2)(3,3)(3,4)(2,4)(1,4) So that, coordinates form a continuous pattern or a path. My question is that,Is there any Matlab algorithm for arranging the Matrix coordinates according to the pattern shown above?................
0 件のコメント
回答 (2 件)
Simon
2013 年 9 月 2 日
There is (to my knowlegde) no matlab function for this, but with a little sorting and geometry this is no problem.
% input matrix
X = [0 0 0 1 0;0 0 0 1 0;0 1 1 1 0;0 1 0 0 0;0 1 0 0 0];
% all filled positions in matrix
[r, c] = ind2sub(size(X), find(X));
% coordinates in 2d, sorted by row index
coord = sortrows([r, c]);
% path along coordinates
coordpath = zeros(size(coord));
% start at first coordinate
coordpath(1,:) = coord(1,:);
coord(1,:) = [];
% loop over all coordinates
for n = 2:length(r)
% vector pointing from last coordinate in path to all other coordinates
vec = coord - ones(size(coord, 1), 1) * coordpath(n-1, :);
% distance to other coordinates
dist = sqrt(sum(vec.^2, 2));
% nearest coordinate has minimum distance
nextcoord = find(min(abs(dist)) == abs(dist));
% add to path
coordpath(n,:) = coord(nextcoord, :);
% remove from coordinate list
coord(nextcoord, :) = [];
end
0 件のコメント
Azzi Abdelmalek
2013 年 9 月 2 日
編集済み: Azzi Abdelmalek
2013 年 9 月 2 日
X=[0 0 0 1 0;0 0 0 1 0;0 1 1 1 0;0 1 0 0 0;0 1 0 0 0];
[n,m]=size(X);
a=reshape(1:n*m,n,m);
a(:,2:2:end)=flipud(a(:,2:2:end));
a=a(:);
[ii,jj]=ind2sub(size(X),a(find(ismember(a,find(X)))));
out=[ii,jj]
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!