obtain a matrix out of other matrix
2 次查看(过去 30 天)
显示 更早的评论
Hello
I have a 1*104 matrix which ı have to create a 8*13 matrix out of it.
For one column of the final 8*13 matrix I can do the following code but how can I write one single code to get the whole matrix
zs_d=zeros(8,1);
for r=1:8;
zs_d(8,1)=(zs(13*r-12));
end
This gives me the first coloumn and for the second column I can write :
for r=1:8;
zs_d(8,1)=(zs(13*r-11));
end
and it goes the same till end...
I tried to write it in one for loop like the following but it gave me an error:
zs_d=zeros(8,13);
for r=1:8,c=1:12;
zs_d(8,13)=(zs(13*r-13-c));
end
error : Subscript indices must either be real positive integers or logicals.
thanks for the help
2 个评论
Fangjun Jiang
2019-10-24
Your code does not match your description. In any case, I think reshape() should help you.
采纳的回答
更多回答(1 个)
Steven Lord
2019-10-24
编辑:Steven Lord
2019-10-24
reshape your vector to be 13-by-8 then take the transpose of it.
>> x = 1:104;
>> y = reshape(x, [13 8])
>> z = y.'
You can do this in one step, but I think it's important to see the intermediate result (y) so you can see how MATLAB arranges elements. This is useful knowledge for a technique called linear indexing.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!