How to get the indices of two matrix simontaneously

2 Ansichten (letzte 30 Tage)
Arvind Kumar Pathak
Arvind Kumar Pathak am 26 Sep. 2017
Bearbeitet: Stephen23 am 26 Sep. 2017
I have two matrix A and B
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15]
B = [1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15]
H = 5
m = size(A, 2); % No of Points on A
l = size(B, 2); % No of Points on B
for j = 1:1:m
x = A(:, j);
for i = 1:1:l
p = B(:, i); %points to test
d = sqrt((p(1, 1) - x(1, 1)) ^ 2 + ...
(p(2, 1) - x(2, 1)) ^ 2 + ...
(p(3, 1) - x(3, 1)) ^ 2);
if d <= H
Q(i) = [i];
Q1(j) = [j];
else
Q(i) = [];
Q1(j) = [];
end
end
end
This is not working
what i actually want to do is, I want save the column indices of A nad B for which the condition of d<=H is satisfied.
Please help me
Thanks

Antworten (2)

KSSV
KSSV am 26 Sep. 2017
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15] ;
B = [1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15] ;
H = 5 ;
m = size(A,2); % No of Points on A
l = size(B,2); % No of Points on B
Qi = zeros(1,[]) ;
Qj = zeros(1,[]) ;
count = 0 ;
for j = 1:1:m
x = A(:,j);
for i = 1:1:l
p = B(:,i); %points to test
d = sqrt((p(1,1) - x(1,1))^2+...
(p(2,1) - x(2,1))^2+...
(p(3,1) - x(3,1))^2);
if d <=H
count = count+1 ;
Qi(count) = i;
Qj(count) = j;
end
end
end

Andrei Bobrov
Andrei Bobrov am 26 Sep. 2017
Bearbeitet: Andrei Bobrov am 26 Sep. 2017
d = squeeze(sum(bsxfun(@minus,B,permute(A,[1,3,2])).^2));
q = d <= H;
Q = bsxfun(@times,(1:size(q,1))',q);
Q1 = bsxfun(@times,q,1:size(q,2));
or
d = squeeze(sum(bsxfun(@minus,B,permute(A,[1,3,2])).^2));
[Q,Q1] = find(d <= H);

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by