On Sep 28, 5:03 pm, "ravi " <ravi_...@hotmail.com> wrote:
> thanks matt,
>
> I am faced with another similar problem:
>
> I am given:
>
> Coords=
> [ 12 23;
> 76 45;
> 29 87]
>
> The answers are supposed to be
>
> F_12 = sqrt((76  12)^2 + (45  23)^2)
> F_13 = sqrt((29  12)^2 + (87  23)^2)
> F_21 = sqrt((12  76)^2 + (23  45)^2)
> F_23 = sqrt((29  76)^2 + (87  45)^2)
> F_31 = sqrt((12  29)^2 + (23  87)^2)
> F_32 = sqrt((76  29)^2 + (45  87)^2)
>
> I'm trying to use bsxfun but is becoming a bit puzzling.
>
> Asking for some more help.
Alright, this probably isn't the most concise way of doing it, but...
it works (for this example, at least...)
s = size(Coords,1);
tmp = [repmat(Coords(:,1),[],s),reshape(repmat(Coords(:,1)',[],s),[],
1),repmat(Coords(:,2),[],s),reshape(repmat(Coords(:,2)',[],s),[],1)];
idx = find(tmp(:,1)tmp(:,2)); %find valid rows...
tmp = tmp(idx,:); %save only valid rows
A = bsxfun(@hypot,tmp(:,1)tmp(:,2),tmp(:,3)tmp(:,4));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A =
67.6756972627545
66.2193325245732
67.6756972627545
63.0317380372777
66.2193325245732
63.0317380372777
Test:
B = [sqrt((76  12)^2 + (45  23)^2)
sqrt((29  12)^2 + (87  23)^2)
sqrt((12  76)^2 + (23  45)^2)
sqrt((29  76)^2 + (87  45)^2)
sqrt((12  29)^2 + (23  87)^2)
sqrt((76  29)^2 + (45  87)^2) ]
%%%%%%%%%%%%%%%%%%%
B =
67.6756972627545
66.2193325245732
67.6756972627545
63.0317380372777
66.2193325245732
63.0317380372777
isequal(A,B)
%%%%%%%%%%%%%%%%%%%
ans =
1
Nathan
