Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to skillfully use Matrix Operation to avoid Circulation in MATLAB

Shulou Source: shulou.com Published: 2022-06-01 06:29:51 09月24日 Update

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.

Tags: Matrix loop operation result knowledge article vector that is form skill method more or not bad practical one previous generation picture above two in use. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Tech Info Shulou Information Microsoft OPPO Reno