Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
problem with division

Subject: problem with division

From: ravi

Date: 28 Sep, 2010 20:50:21

Message: 1 of 6

Good day,

I have a matrix A as follows:

A = [ 23;
          11;
          87]

I'd like to create divisions of all elements as follows:

23/11
23/87
11/23
11/87
87/23
87/11

So the output answer would look like:


Result = 2.0909
                  0.26437
                   0.47826
                   0.12644
                    3.7826
                    7.9091

How can I do this?

thanks

Subject: problem with division

From: Matt Fig

Date: 28 Sep, 2010 21:03:22

Message: 2 of 6

One approach:

A = bsxfun(@rdivide,A.',A);
A(1:size(A,1)+1:end) = []

Subject: problem with division

From: ravi

Date: 29 Sep, 2010 00:03:09

Message: 3 of 6

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.

Subject: problem with division

From: Nathan

Date: 29 Sep, 2010 00:29:16

Message: 4 of 6

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

Subject: problem with division

From: ravi

Date: 29 Sep, 2010 01:09:04

Message: 5 of 6


> Alright, this probably isn't the most concise way of doing it


thanks nathan,

it works, can you let me know why perhaps it isn't the most concise way of doing it?

Subject: problem with division

From: Roger Stafford

Date: 29 Sep, 2010 06:50:20

Message: 6 of 6

"ravi " <ravi_071@hotmail.com> wrote in message <i7tvnt$2du$1@fred.mathworks.com>...
> .......
> 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.
- - - - - - - - - -
  You don't need bsxfun for this problem.

 n = size(C,1); % Where C is your "Coords"
 p = (1:n^2).';
 p(1:n+1:n^2) = [];
 [I,J] = ind2sub([n,n],p); % Convert to indices in C
 F = sqrt(sum((C(I,:)-C(J,:)).^2,2)); % Where F is your F_12, F_13, etc.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us