from an array, creating a row vector of elements but one row is in reverse order

Starting with
>> y=[5 6 7 0 -2; 1 5 6 5 3; -6 -2 0 -3 -5; 0 1 1 6 8]
y =
5 6 7 0 -2
1 5 6 5 3
-6 -2 0 -3 -5
0 1 1 6 8
What is the one-line command to create a 1-by-10 row vector containing the elements of the second row and third row, where the third row has been reversed left-to-right

8 Comments

  1. Extract the second row from y.
  2. Extract the third row from y.
  3. Reverse the third row.
  4. Concatenate the second row and the reversed third row to a new row vector.
As you like it.
Since you are a beginner, why not using 4 lines (one for each step) ?
Im supposed to do it in one line, im just not sure how and I cant find any examples
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.

Sign in to comment.

Answers (3)

If it's not your homework, try this:
y=[5 6 7 0 -2; 1 5 6 5 3; -6 -2 0 -3 -5; 0 1 1 6 8]
y = 4×5
5 6 7 0 -2 1 5 6 5 3 -6 -2 0 -3 -5 0 1 1 6 8
result = [y(2,:), fliplr(y(3,:))]
result = 1×10
1 5 6 5 3 -5 -3 0 -2 -6
If it is your homework and you turn in my code as your own, you could get into trouble if you get caught by your instructor.
Eg.:
A = [ 1, 2 3; 4 5 6; 7 8 9]
% Extract Row 1
a1 = A(1,:);
% Extract Row 3
a3 = A(3.:);
% Reverse Row 1
a1 = fliplr(a1)
% Concatenate
B = [a1, a3]
% Now apply these steps with your exercise in one row of command by
% combining these three steps together

Products

Release

R2022a

Asked:

on 13 Jan 2023

Commented:

on 13 Jan 2023

Community Treasure Hunt

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

Start Hunting!