Got Questions? Get Answers.
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:
Matrix form of Vector differences

Subject: Matrix form of Vector differences

From: Zeph Davies

Date: 13 Mar, 2013 10:48:06

Message: 1 of 2

Hi,

I know that for a Vector A: A'*A creates a Matrix of all the elements of A multiplied by each other.
Is there a similar method for adding/subtracting, without looping?
I guess you have to turn the Vector into a repeated matrix, and then find the sum/difference.

Thus far i have:
>> a = [1 2 3 4 5]

a =

     1 2 3 4 5

>> b = a'*ones(1,length(a))

b =

     1 1 1 1 1
     2 2 2 2 2
     3 3 3 3 3
     4 4 4 4 4
     5 5 5 5 5

>> c = ones(1,length(a))'*a

c =

     1 2 3 4 5
     1 2 3 4 5
     1 2 3 4 5
     1 2 3 4 5
     1 2 3 4 5

>> c-b

ans =

     0 1 2 3 4
    -1 0 1 2 3
    -2 -1 0 1 2
    -3 -2 -1 0 1
    -4 -3 -2 -1 0


Is there a better / less verbose method?

Zeph

Subject: Matrix form of Vector differences

From: Zeph Davies

Date: 13 Mar, 2013 11:22:06

Message: 2 of 2

Scratch that, just found it:

bsxfun(@minus,a,a')

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