Coding a complicated summation.

3 views (last 30 days)
rj
rj on 27 May 2017
Commented: Jan on 28 May 2017
Hello all,
I am very new to matlab and am struggling to code the following: I have the following matrices: 4 by 1
X = [x_1 x_2 x_3 x_4]', [u_1 u_2 u_3 u_4]'
Divide them into two groups:
[x_1 x_2], [u_1 u_2] and
[x_3 x_4], [u_3 u_4]
and I want the following sum:
[x_1 x_2]*[u_1 u_2]'[u_1 u_2]*[x_1 x_2]'+
[x_1 x_2]*[u_1 u_2]'[u_3 u_4]*[x_3 x_4]'+
[x_3 x_4]*[u_3 u_4]'[u_1 u_2]*[x_1 x_2]'+
[x_3 x_4]*[u_3 u_4]'[u_3 u_4]*[x_3 x_4]'
  • = matrix multiplication.
I know how to code if we are summing over x_iu_j for i from 1 to 4 and j = 1 to 4.. but dont know how to do this sum when you group the observations.. Upon grouping I only know how to code :
[x_1 x_2][u_1 u_2]'[u_1 u_2][x_1 x_2]'+[x_3 x_4][u_3 u_4]'[u_3 u_4][x_3 x_4]'.
Any suggestions would be much appreciated. Thank you.
  3 Comments
rj
rj on 27 May 2017
I updated my question.. hope it is more clear now.. thank you for noticing...
Jan
Jan on 28 May 2017
Sorry, it is still unclear:
[x_1 x_2]*[u_1 u_2]'[u_1 u_2]*[x_1 x_2]'
Please use proper Matlab syntax.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 27 May 2017
You seem to have coded what you want to do efficiently.
You only need to include multiplication operators (and possibly some others) to do the dot-products and other operations (see the documentation on Array vs. Matrix Operations (link)), then create your code as a function. See the documentation on Function Basics (link) if you are unfamiliar with them.
  3 Comments
Walter Roberson
Walter Roberson on 27 May 2017
Would the matrices become 400 x 1 or 1600 x 1, or is it that you have 400 or 600 of those 4 x 1 matrices?
Star Strider
Star Strider on 27 May 2017
My pleasure.
If you are operating on a small number of elements in your code each time to get the summations, it might be easiest to create a function from your code, then call it in a loop with the appropriate inputs (arguments). With a function, it would also be easier to be certain the code is doing what you want, before using it on a large data set.
Also, what operators go here?
[x_1 x_2][u_1 u_2]'[u_1 u_2][x_1 x_2]'
What result do you want?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!