Combining multiple rows from one matrix to another matrix

2 views (last 30 days)
Hi..I have a P matrix like this:
Hi,
Let's say I have a P matrix (10*5) and I want to get Q matrix (5*10) from this P matrix where
first two rows of P matrix will be the first row of Q;
rows 3 and 4 of P will be the 2nd row of Q;
rows 5 and 6 will be the 3rd row of Q;
and so on so forth.
Original P matrix is:
P=[1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
36 37 38 39 40
41 42 43 44 45
46 47 48 49 50];
I want to get following Q matrix:
Q=[1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50];
Can anyone please help how to get this Q from P?

Answers (2)

Star Strider
Star Strider on 8 Apr 2024
Use the reshape function —
P=[1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
36 37 38 39 40
41 42 43 44 45
46 47 48 49 50];
Q = reshape(P.', 2*size(P,2), []).'
Q = 5x10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
.

Hassaan
Hassaan on 8 Apr 2024
% Define the original matrix P
P = [1 2 3 4 5;
6 7 8 9 10;
11 12 13 14 15;
16 17 18 19 20;
21 22 23 24 25;
26 27 28 29 30;
31 32 33 34 35;
36 37 38 39 40;
41 42 43 44 45;
46 47 48 49 50];
% Reshape P into Q
Q = reshape(P', [10, 5])'; % Transpose P, reshape it, and then transpose the result back
% Display the result
disp('Matrix Q:');
Matrix Q:
disp(Q);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!