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