Info

この質問は閉じられています。 編集または回答するには再度開いてください。

modifying vectors help.

1 回表示 (過去 30 日間)
Naeem Abdulla
Naeem Abdulla 2019 年 2 月 21 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
i have having some trouble changing a vector.
here is the issue
i need to change
x=[1:5]
which is
x=1 2 3 4 5
into
8 6 4 2 0
assigned to a variable called x1
need to go from
>>x=[1:5}
x= 1 2 3 4 5
.
.
.
.
To
x3= 8 6 4 2 0
however i must do it using the variable x and a formula. i know how to change the values of the numbers using
>> x(1:5)=[8:-2:0]
x =
8 6 4 2 0
but i must do it by using a furmula and the variable x. anyone can help?
  1 件のコメント
John D'Errico
John D'Errico 2019 年 2 月 21 日
編集済み: John D'Errico 2019 年 2 月 21 日
Did I not see this identical problem recently, yet in a slightly different form? Let me say that differently. I DID see this problem posed as a homework question, a couple of weeks ago, although then it was somewhat disguised by mathematics.
x =
1 2 3 4 5
y =
1 256 729 256 25
That is, find a simple formulaic remapping of vector x into y.

回答 (1 件)

John D'Errico
John D'Errico 2019 年 2 月 21 日
編集済み: John D'Errico 2019 年 2 月 21 日
When all else fails, throw it at polyfit. (Well, actually, I know polyfit will work here, so it really is not a code of last resort.)
x = 1:5;
y = [8 6 4 2 0];
polyfit(x,y,1)
ans =
-2 10
I will assert that is the answer. But now you need to figure out what is the question.
Yes, there may arguably be conceptually easier ways to do this. But polyfit is actually an elegant way to solve the problem.
So, think about what use of polyfit means here. What model does polyfit build in this case? Can you write down that model? Then substitute the estimated coefficients as returned from polyfit. Come on. Try it.

Community Treasure Hunt

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

Start Hunting!

Translated by