Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个数据:
data=[1 2 3 4 5 6 7 8];
我需要一个新的数据矩阵(4 x 8),如下所示:
new_data =[ 1 2 0 0 0 0 0 0 0 0 3 4 0 0 0 0 0 0 0 0 5 6 0 0 0 0 0 0 0 0 7 8 ]
如何使用 FOR 循环来做到这一点?有什么帮助吗?
无需使用 for 循环
data = 1:8; newdata = [reshape(data,2,4); zeros(8,4)]; newdata = reshape(newdata(1:32), 8, 4)';
如果需要,这是一个使用循环的解决方案
clear('newdata'); for ii = 1:4 index = 2*(ii-1)+1:2*ii; newdata(ii,index) = data(index); end