How can I flip a row vector without using flip(lr) function?

I want to write a function that it can flip a row vector without flip(lr) function.
if true
% A=[1 2 -3 4]
And output must be:
B=[4 -3 2 1]
end

 採用された回答

Star Strider
Star Strider 2018 年 11 月 4 日

1 投票

Reverse the indices:
A = [1 2 -3 4];
B = A(numel(A):-1:1)
B =
4 -3 2 1

5 件のコメント

denizakyol
denizakyol 2018 年 11 月 4 日
Thank you.
Star Strider
Star Strider 2018 年 11 月 4 日
As always, my pleasure.
Image Analyst
Image Analyst 2018 年 11 月 4 日
You can simply use "end" - you don't need to call the numel() function:
B = A(end:-1:1)
end still works inside the parentheses even though it's the first thing in there instead of the last.
Star Strider
Star Strider 2018 年 11 月 4 日
@Image Analyst — Good point. Thank you.
I wanted to make my code straightforward, the reason I wrote it as I did. I was not certain if using end would do that.
denizakyol
denizakyol 2018 年 11 月 4 日
@Image Analyst Thank you so much. The end function is so helpful at all matrix operations. I would not forget anymore.

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

その他の回答 (3 件)

madhan ravi
madhan ravi 2018 年 11 月 4 日

0 投票

A=[1 2 -3 4]
B=wrev(A)

1 件のコメント

denizakyol
denizakyol 2018 年 11 月 4 日
Thank you but I don't have Wavelet Toolbox. I want to write this function with matrix operations.

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

Saad
Saad 2022 年 10 月 15 日

0 投票

how to conver a matrix without using (flip function's)?for example
a= 1 2 3 4 b=4 3 2 1
5 6 7 8 8 7 6 5
9 10 11 12 12 11 10 9
13 14 15 16 16 15 14 13

2 件のコメント

Star Strider
Star Strider 2022 年 10 月 15 日
@Saad
The same as I originally posted, with an additional row dimension —
a = [1:4; 5:8; 9:12; 13:16]
a = 4×4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
b = a(:,size(a,2):-1:1)
b = 4×4
4 3 2 1 8 7 6 5 12 11 10 9 16 15 14 13
.
Stephen23
Stephen23 2022 年 10 月 15 日
b = a(:,end:-1:1)

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

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

質問済み:

2018 年 11 月 4 日

コメント済み:

2022 年 10 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by