i want to extract rows from a matrix
5 views (last 30 days)
Show older comments
Mahmoud Chawki on 16 May 2022
Answered: Animesh Gupta on 8 Jun 2022
i have a matrix that the number of rows are always even.
i want a code that extracts 2 rows and put them together
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?
DGM on 16 May 2022
Edited: DGM on 16 May 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]
k = repelem(1:size(A,1)/2,1,2).'
B = A.*k
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.
Animesh Gupta on 8 Jun 2022
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
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
% You can access the individual 2d sub-arrays as:
I hope it helps.
Find more on Creating and Concatenating Matrices 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!