Clear Filters
Clear Filters

finding nearest vlaue

3 views (last 30 days)
kash
kash on 6 Apr 2012
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

Accepted Answer

Jonathan Sullivan
Jonathan Sullivan on 6 Apr 2012
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)

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 6 Apr 2012
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 Comments
Andrei Bobrov
Andrei Bobrov on 6 Apr 2012
see in Veriable Editor
kash
kash on 6 Apr 2012
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!