In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how MATLAB skillfully uses matrix operations to avoid loops. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Untitled
I suddenly remembered this method when I was doing my homework at noon. I felt that it should be useful, so I made this push while it was hot to share it, ha.
Well, that's the question in the red box in the picture above.
To figure it out, I was going to use a loop to calculate it, but a flash flashed through my mind at that time, and then the idea of using a matrix to calculate it came to me. Haha, but luckily I still remember some knowledge of linear algebra and tried to figure it out. Let's put the code first and then explain it briefly.
A1 = 1
A2 = 0.5
A3 = 0.2
A = [A1, A2, A3]
F0 = 5;% Hz
I = [1:3]'
X3 = @ (t) A * sin (2 times pirated f0 * i* t)
Fs = 2048;% sampling frequency Hz, 2048 points per second
Ts = 1 / fs;% sampling interval
N = 0: 2048-1;% 2k points (exactly 1s)
T = n. * Ts;% time t
Plot (t, x3 (t))
Here are the results.
Visual x3
Now look at the formula in the question, which is the sum of the following three lines:
This can be written as
It's not over yet.
The above matrix can be written in the following form
Hey, so the sum in that question can be written in the following form
This is the line of the program.
X3 = @ (t) A * sin (2 times pirated f0 * i* t)
Note that An is the row vector and I is the column vector. This is the end of this little trick, in fact, it is still mathematical knowledge, not a small skill. Ai, it's so easy to use the latex insertion formula in this MarkDown. Praise it.
But, if you don't remember the multiplication of the matrix in this line generation at all, and you have no confidence in the above method, use a loop, as follows:
X = 0
For I = 1: length (A)
X = x + A (I). * sin (2*pi*f0*i. * t)
End
Sum (x = = x3 (t))% is compared with the result from the matrix operation above to see if the two values are the same.
The output of this on the command line is as follows
Haha, it shows that these 2048 points are all the same, that the result calculated by matrix operation above is correct, be confident, . This is the end of the article on "how to skillfully use matrix operations to avoid cycles in MATLAB". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.