Coding a complicated summation.

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 个评论

What does this mean:
[x_1 x_2][u_1 u_2]'[u_1 u_2][x_1 x_2]'
It is no valid Matlab syntax, therefore we have to guess the meaning, what is not reliably. Better explain clearly, what you are looking for. Thanks.
I updated my question.. hope it is more clear now.. thank you for noticing...
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.

请先登录,再进行评论。

回答(1 个)

Star Strider
Star Strider 2017-5-27

0 个投票

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 个评论

Thank you for the comment. However, I want to do this summation over large number of observations.. so i basically sums from 1 to like 400 or even 1600.. I think I need some kind of loop here.
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?
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?

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

提问:

rj
2017-5-27

评论:

Jan
2017-5-28

Community Treasure Hunt

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

Start Hunting!

Translated by