Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

modifying vectors help.

1 vue (au cours des 30 derniers jours)
Naeem Abdulla
Naeem Abdulla le 21 Fév 2019
Clôturé : MATLAB Answer Bot le 20 Août 2021
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 commentaire
John D'Errico
John D'Errico le 21 Fév 2019
Modifié(e) : John D'Errico le 21 Fév 2019
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.

Réponses (1)

John D'Errico
John D'Errico le 21 Fév 2019
Modifié(e) : John D'Errico le 21 Fév 2019
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