How can I create 3D matrix from 2D matrix by diagonal of coulumn variables?

1 次查看(过去 30 天)
I have a pr(17859x10957) matrix. I would like to create a 17859x17859x10957 matrix by the diagonal of every column. I tried the following solution:
for j=1:10957
rr(:,:,j)=diag(pr(:,j));
clear rr
end
This solution consumes too much physical memory. Can someone offer me a better solution?
  1 个评论
Matt J
Matt J 2015-5-19
I don't think you want the "clear rr" command in there. That will erase the hard work done by every pass through the loop. It will also de-allocate rr (I hope you have pre-allocated it before the loop) making the loop run very slowly.

请先登录,再进行评论。

回答(1 个)

Matt J
Matt J 2015-5-19
Depending on what you plan to do with rr, you could use my ndSparse class to maintain it in sparse form,

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by