Insert rows and columns of zeros between every row column

£可爱£侵袭症+ 提交于 2019-12-01 07:29:54

问题


If i have a MxN matrix, how do i add(not replace) a row of zeros and a column of zeros after every other column/row in the original matrix in matlab? Effectively the result would be 2Mx2N.


回答1:


You can do it in the following way. Do not add the new rows and columns but create an empty matrix and fill the elements from the original matrix.

Create a new matrix with the dimensions 2Mx2N

 B = zeros(2*size(A));

(assuming that A is your original matrix). Using

 B(1:2:end,1:2:end) = A;

should result in the correct new matrix.



来源:https://stackoverflow.com/questions/41997908/insert-rows-and-columns-of-zeros-between-every-row-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!