user30609
user30609

Reputation: 264

divide every element by every other to create a matrix MATLAB

If I have a vector of numbers A = 1 2 3

Is it possible to create a matrix with

A = [ 1/1 1/2 1/3;
      1/2 2/2 2/3;
      1/3 2/3 3/3;]

I naively tried 

%%
a = 1:3;

aa = a./a(:); %This is what I naively tried

A = [ 1/1 1/2 1/3;
      1/2 2/2 2/3;
      1/3 2/3 3/3;]

aa was what I naively tried

Upvotes: 1

Views: 74

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112659

You only need

A = min(aa,aa.');

where aa is computed as in your question.


Mostly for fun, you could also abuse pdist (Statistics Toolbox) for this:

A = exp(-squareform(pdist(log(a(:)))));

where a = 1:3 as in your question.

Upvotes: 5

Related Questions