1 5 2 1 laff copy 1 (Matlab) (2015) - 雙語字幕

Let's implement our first function.
It's going to be a very simple one that copies a factor to not a factor.
We're going to implement it using MScript, which the language that Matlab uses.
Now, realize
that I'm making this video as the materials are still on the construction,
so it may all look slightly different from what I'm showing, but you'll get the gist of it.
So let's get started on the first programming assignment, which is to create a function in MScript that copies one vector to another.
We have rather detailed instructions for this,
so if you go to the notes and you find this homework and you click on laugh programming supplement,
you will get there, and it'll look something like this, now.
What I want you to do is to start up MATLAB and what you will see is something
like this and you may have to navigate to the particular sub-directory that has the programming assignments in it.
We talked about that in week zero.
And to do so, you may have the browse for the folder to get there.
Anyway, let's instead start right here.
Now the instructions say to start by creating two vectors x and y.
So let's go to Matlab and in the command window, which is this window right here.
Let's create a vector x, 1, 2, 3, and a vector y, 0, minus 1, minus 2.
And you can always look at the contents of vectors x and y again by just saying x.
it'll print it.
Now what we're going to do is we're going to back up these vectors.
So we're going to say x old is equal to x.
And notice that in the process, we're copying one vector to another.
So in some sense, intrinsic in MATLAB is already the ability to assign one vector to another.
But we're going to write because we're going to learn from doing so.
So set x-alt to x and we can set y-alt to y and notice that it's a little annoying that every time the contents are
printed and you can suppress that by putting a semicolon at the end of the line.
We now look at y-old, you see that has the contents of y.
Now, to copy one vector to another, what one can do
is simply assign an element of x to the corresponding element of y one at a time.
Now, to access an element of x, to access an element of x, you say x of 1.
Now, recalling that x is 1, 2, 3, x of 1 is the first element of x.
The first element of y has contents 0, because y had contents 0, minus 1, minus 2.
And what you notice is that M script starts indexing at 1,
while we would have called y of 1 actually size sub 0 in our notation in our course.
And that's just because we choose to start indexing at 0,
as scientists tend to do, while MATLAB starts indexing at which is what mathematicians tend to do.
Okay, so here we have it.
And to now assign the first element of x to the first element of y, we could do this.
What you notice is that it's being overwritten.
And then we could do the same thing for the second elements.
for the third elements.
So let's reset Y to the original value.
Obviously if these vectors were of length 100, this would have been rather tiresome to do the copy one at a time.
M script has something that's called a for loop.
And what you can say is,
for in the C's I from one,
two, three, set Y of I equal to X of I, and you have to say end, which is the end of the span of the for loop.
And what happens is that one by one, the elements of y are overwritten with the corresponding elements of x.
That's reset y to y old.
We could have done the same thing,
suppressing the printing of the intermediate results by saying y of y of
i is equal to x of i semicolon and then if we print out y you see that it has been overwritten.
Now again obviously every time you want to copy one vector into another it would be a little bothersome to have to loop.
And therefore, what we're going to do is we're going to create a function.
Now, to do that, we're going to start by going into subdirectory programming, and into subdirectory
week one, which is where we're going to put the files that we're going to create in week one.
and we're going to go up here and say new function and what we get here is
an outline for a function notice that it says function then some output
arguments some name of a function and then some input arguments now the name of the
function we're going to choose to be left on this core copy input arguments are going to be both x and y.
And the output argument is going to be y sub out.
And then you really should have some comments here that describe what the function does.
Let's save this.
And we're going to save this into a file, laugh underscore copy.m.
Notice that that's what the matlab files have as an ending, and you say save.
And now over here we could actually call this function.
So let's reset y to be y old,
and we could now Hmm,
let's assign to y out the copy copying of x into y, but notice that we're actually saying put the result into vector y out.
And actually if we do this it protests because it says wait a minute,
as part of this function we're saying that we're going to create an output variable y out and we never assign anything to it
and that just looks a little hokey.
So what we're going to do now is we're going to say let's set y out to be a vector of zeros that are simple.
3 by 1.
So we're actually viewing column vectors as 3 by 1 arrays.
Let's then assign for i equals 1 through 3, 2 y out of i, the of x of i.
And another is we're not even using the input variable y.
What's going on with that will become clear as you go through the programming supplement in more detail.
Now I like to put a return at the end here to indicate that we are returning from the function.
We now save this and I'm doing that with command s, or I can just say save.
Then if I now say why out is equal to live cloth copy x comma y,
notice that why out ends up being overwritten with the content But,
I could just as easily set y equal to left copy x comma y, in which case y gets overwritten.
Let's reset y to y old.
And if we now do this,
but we put a semicolon at the end, ah, then notice along the way, the intermediate values of y out are being written.
And if we then say why, we also see that y has been updated.
So what we really want to do here is we want to put a semicolon at the end to suppress the printing of intermediate results.
Let's save this.
And now what we have is a routine that can copy a vector of length 3 into another vector of length 3.
Now obviously we probably want a more general function that can copy a vector of length n into a vector of length n.
So let's change this now.
Okay, for that we're going to use the m script function size.
If look at size of x, it tells you that it's a 3 by 1 array.
That's because we view a column vector as a 3 as an n by 1 array.
And what we can do is we can extract the first and second dimension of the vector as such.
And what that then does is it returns the row and column dimension.
And then we can use that in our function by saying, let's let mx.
nx equal to the size of x, semicolon to suppress printing.
Let's let our output vector be of size mx by 1,
and then for i equals 1 to m sub x,
let's set the i-th entry equal to the corresponding entry in x,
and if we now save this, then we have a function that can copy in general.
If we now set x equal to 1,
2, 3, 4, and we said y equal to 0 minus 1 minus 2 minus 3, and we call
copy routine,
let's see, I'm going up here with the up arrow, then notice that the vector of length 4 has been copied from x into y.
Now, it turns out that we really want a function that can copy a row vector into a column vector
or column vector into a row vector,
etc., etc., etc., and the rest of the programming supplement on this particular homework problem walks you through all of those steps.
翻譯語言
選擇翻譯語言

解鎖更多功能

安裝 Trancy 擴展,可以解鎖更多功能,包括AI字幕、AI單詞釋義、AI語法分析、AI口語等

feature cover

兼容主流視頻平台

Trancy 不僅提供對 YouTube、Netflix、Udemy、Disney+、TED、edX、Kehan、Coursera 等平台的雙語字幕支持,還能實現對普通網頁的 AI 劃詞/劃句翻譯、全文沉浸翻譯等功能,真正的語言學習全能助手。

支持全平臺瀏覽器

Trancy 支持全平臺使用,包括iOS Safari瀏覽器擴展

多種觀影模式

支持劇場、閱讀、混合等多種觀影模式,全方位雙語體驗

多種練習模式

支持句子精聽、口語測評、選擇填空、默寫等多種練習方式

AI 視頻總結

使用 OpenAI 對視頻總結,快速視頻概要,掌握關鍵內容

AI 字幕

只需3-5分鐘,即可生成 YouTube AI 字幕,精準且快速

AI 單詞釋義

輕點字幕中的單詞,即可查詢釋義,並有AI釋義賦能

AI 語法分析

對句子進行語法分析,快速理解句子含義,掌握難點語法

更多網頁功能

Trancy 支持視頻雙語字幕同時,還可提供網頁的單詞翻譯和全文翻譯功能

開啟語言學習新旅程

立即試用 Trancy,親身體驗其獨特功能