# have been practicing questions i see online i just cant get how to solve this mathwork

1 回表示 (過去 30 日間)
IBRAHIM DIBAL 2022 年 6 月 15 日

Write a function called flip_it that has one input argument, a row vector v , and one output argument, a row vector w that is of the same length as v. The vector w contains all the elements of v, but in the exact opposite order. For example, if v is equal to [1 2 3] then w must be equal to [3 2 1]. You are not allowed to use the built-in function flip, fliplr, or flipud.
##### 3 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
DGM 2022 年 6 月 15 日
I will note that they didn't explicitly say you couldn't use flipdim().
But I suppose that's not good enough.
Consider a vector
v = [12 35 48 63];
it has the linear indices:
idx = 1:numel(v);
idxr = numel(v):-1:1;
What would happen if you used those indices to address v?
Sam Chak 2022 年 6 月 15 日
Do you think that sort is easier for you to rearrange the elements?
sort()

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

### 回答 (1 件)

Chandrika 2022 年 6 月 17 日
As per my understanding, you need to code for a function that reverses a vector. You can do this using a recursive function. Please try the following code:
function w=flip_it(v)
w=[];
if isequal(length(v),1)
w=v(1);
elseif length(v) > 1
st=v(1:ceil(length(v)/2));
ed=v(ceil(length(v)/2)+1:end);
w=[flip_it(ed) flip_it(st)];
else
return
end
end
Here, recursion stops when length of the vector is 1, else flip_it function keeps getting called as part of the recursion.

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

### カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by