Coding a complicated summation.
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jan
2017-5-28
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
2017-5-27
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 个评论
Walter Roberson
2017-5-27
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
2017-5-27
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!