简体   繁体   中英

How do I create this matrix with these conditions in matlab?

I would like to create a matrix w_prev from a matrix wt with these conditions.

  1. wt is a 4x100 matrix.
  2. w_prev is a 1x4*N matrix with N being an integer. The first 4 rows of w_prev are the 4 elements in the first column of wt , second 4 rows of w_prev are the 4 elements of the second column of wt etc. until integer N.

In this example, N is 3 which works fine. k is 1 by the way.

w_prev=[wt(:,k);wt(:,k+1);wt(:,k+2)]

I would like to program it for generic integer N.

You can use the colon operator certain columns of the matrix. Here you want column k to k+2:

wt(:,k:k+2)

This already returns the right elements, but in the wrong shape. It's a 4x3. Use reshape to reshape it into a vector:

reshape(wt(:,k:k+2),[],1)

For generic N:

reshape(wt(:,k:k+N-1),[],1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM