finding nearest vlaue

조회 수: 4 (최근 30일)
kash
kash 2012년 4월 6일
I have an matrix
A=[1 2 8
7 9 6
10 14 89]
now i want to find the nearest value for example,lets ur assume 2,the nearest values are 1,7,9,8,6
for 1 it is 2,7,9
please tell how to find the nearest value

채택된 답변

Jonathan Sullivan
Jonathan Sullivan 2012년 4월 6일
Kash,
You might want to try this. It'll allow for you to do the neighbors of more than 1 number at a time. Just change the variable val to be what you want (either a scalar, or a vector).
A = [1 2 8
7 9 6
10 14 89];
val = [1 2];
[y x] = find(ismember(A,val));
ys = unique(bsxfun(@plus,y,-1:1));
xs = unique(bsxfun(@plus,x,-1:1));
ys(ys <= 0 | ys > size(A,1)) = [];
xs(xs <= 0 | xs > size(A,1)) = [];
A(ys,xs)

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2012년 4월 6일
A=[1 2 8
7 9 6
10 14 89]
a = zeros(3);
out = cell(numel(A),2);
for i1 = 1: numel(A)
b = a;
b(i1) = 1;
out(i1,:) = {A(i1),A(bwdist(b,'chessboard') == 1)};
end
OR
B = nan(size(A)+2);
B(2:end-1,2:end-1) = A;
m1 = bsxfun(@plus,(1:3).',(0:2)*size(B,1));
i1 = bsxfun(@plus,m1(:),m1(:).'-1);
B1 = B(i1);
ic = ceil(size(i1,1)/2);
out = [B1(ic,:)' sort(B1([1:ic-1,ic+1:end],:).',2)];
With show?
B = nan(size(A)+2);
B(2:end-1,2:end-1) = A;
m1 = bsxfun(@plus,(1:3).',(0:2)*size(B,1));
i1 = bsxfun(@plus,m1(:),m1(:).'-1);
B1 = B(i1);
ic = ceil(size(i1,1)/2);
B2 = arrayfun(@(ii)reshape(B1(:,ii),size(A,1),[]),(1:size(B1,2))','un',0);
t2 = cellfun(@(x)~isnan(x),B2,'un',0);
out = [num2cell(B1(:,ic)),cellfun(@(x,y)x(any(y,2),any(y)),B2,t2,'un',0)]
  댓글 수: 3
Andrei Bobrov
Andrei Bobrov 2012년 4월 6일
see in Veriable Editor
kash
kash 2012년 4월 6일
thanks andrei can u please tell how to send a packet daat from one node to another ,i have a node and its neighbours
assume node 2 has neighbours 5 ,7, 9,please tell how to send packet data from one node to each node and packet contains
[, S, D , B, C, “TIME”, UUID]
S- source
D- destination
B- bit rate
C- formatting cost
I – “me” node
J – “you” node
UUID – packet ID
please tell how to process these

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by