How to write a script function matlab called myflip?

1 回表示 (過去 30 日間)
Kiyoko Christian Mwizu
Kiyoko Christian Mwizu 2020 年 10 月 20 日
コメント済み: KSSV 2020 年 10 月 20 日
%this code tests the function, myflip, which you will write below
v1 = 100*rand(1);
v1 = myflip(v1)
n = randi([2 100], 1, 1);
v2 = 100*rand(1,2*n);
v2 = myflip(v2)
n = randi([2 100], 1, 1);
v3 = 100*rand(2*n+1,1);
v3 = myflip(v3)
Where indicated in the script below, write a function, called myflip, which accepts one vector, v, (either a column or row), and outputs the same vector, v, of the same dimensions, but with the values in reverse order (as the MATLAB built-in flip() function does). In other words, v will be overwritten by its flipped version. In your function, you may use only the built-in MATLAB functions, length( ) and floor(), the latter of which will be useful for stopping the loop. (You need only one loop here, as opposed to nested loops.)
I need help on this question. I am stuck.

回答 (1 件)

KSSV
KSSV 2020 年 10 月 20 日
x = rand(1,10 ) ;
xr = x ;
n = length(x) ;
for i = 1:n
i
xr(i) = x(n-i+1) ;
end
xr
  4 件のコメント
Kiyoko Christian Mwizu
Kiyoko Christian Mwizu 2020 年 10 月 20 日
Thtas not how its supposed to be. It has to be a script that I have to run and I will use the test provided above to check if the function is working or not. You just cannot assign a random value.
KSSV
KSSV 2020 年 10 月 20 日
x is some data I have chosen for demo.....you have to replace that with your required array.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by