i want to extract rows from a matrix

i have a matrix that the number of rows are always even.
i want a code that extracts 2 rows and put them together
for example:
s=[Row1;Row2;Row3;Row4;Row5;Row6]
s is a matrix that has six rows
i want to extract row 1 and row 2, put them together.
row 3 and row 4, put them together.
row 5 and row 6, put them together.
how can i achieve this?

5 comentarios

Given this matrix:
s = randi(9,6,3)
s = 6×3
7 9 6 9 1 5 2 9 6 9 2 1 4 7 5 7 8 8
please show the expected output.
Mahmoud Chawki
Mahmoud Chawki el 16 de Mayo de 2022
This is the output i want
A(1)=7 9 6
9 1 5
A(2)=2 9 6
9 2 1
A(3)=4 7 5
7 8 8
because i was to take each2 rows seperately and multiply them by a number
Stephen23
Stephen23 el 16 de Mayo de 2022
Editada: Stephen23 el 16 de Mayo de 2022
"because i was to take each2 rows seperately and multiply them by a number "
It might be easier to do that directly. Please show the expected output of the multiplication.
Mahmoud Chawki
Mahmoud Chawki el 16 de Mayo de 2022
The problem is that this matrix varies in rows, it depends on a number which is number of plies if number of plies is 3 this matrix will have 6 rows if number of lies is 4 this matrix will have 8 rows so you see it is alternating and depends on the input that the user uses
DGM
DGM el 16 de Mayo de 2022
Editada: DGM el 16 de Mayo de 2022
You're going to have to define the factors either way, so the fact that the array may differ in size doesn't matter. The problem is that we still don't know what you're actually trying to multiply with what and what the final output is.
For instance, if you want every pair of rows multiplied by a scalar:
A = [7 9 6; 9 1 5; 2 9 6; 9 2 1; 4 7 5; 7 8 8]
A = 6×3
7 9 6 9 1 5 2 9 6 9 2 1 4 7 5 7 8 8
k = repelem(1:size(A,1)/2,1,2).'
k = 6×1
1 1 2 2 3 3
B = A.*k
B = 6×3
7 9 6 9 1 5 4 18 12 18 4 2 12 21 15 21 24 24
Note that this example works regardless of how many rows A has. Generating k as a simple linear ramp is probably not what you want, but you haven't said what you want the factors to be.

Iniciar sesión para comentar.

Respuestas (1)

Animesh Gupta
Animesh Gupta el 8 de Jun. de 2022
Hi,
It is my understanding that you want to extract adjacent rows of a matrix.
You may refer the following code snippet that demonstrates a procedure to extract adjacent rows.
mat = rand(10,5); % creating an array using rand method
disp(mat);
num_of_rows = size(mat,1); % using size method to get the dimensions of matrix along axis 1
new_mat = [];
for i = 1:num_of_rows-1
if mod(i,2) == 1
new_mat = cat(3, new_mat, [mat(i,:); mat(i+1,:)]); % using cat method to append along the 3rd dimension of the new matrix
end
end
% You can access the individual 2d sub-arrays as:
disp(new_mat(:,:,1));
disp(new_mat(:,:,2));
I hope it helps.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 16 de Mayo de 2022

Respondida:

el 8 de Jun. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by