MATLAB更快地重新映射索引

时间:2014-02-01 15:57:44

标签: performance algorithm matlab for-loop matrix

嗨,我对Matlab很陌生,并且在原始矩阵中将值重新映射到另一个矩阵中的新点时非常天真。

我有矩阵Old_matrix(MxNxP),我需要将Old_matrix(m,n,p)的每个值重新映射到new_matrix(M,length(n)* sind(p)),length(n)* cosd(p))。

到目前为止我的代码是

% m,n,p are the dimensions of old_matrix.
% m = 560, n = 360, p = 45
% old_matrix is defined originally as 
old_matrix = randi(255,560,360,45);
% 

% 
new_matrix = zeros(560, 360, 360*cosd(45)+10);
for m_index = 1:560
    for n_index = 1:360
        for p_index = 1:45
            new_m = m_index;
            new_n = round(n_index*sind(p_index));
            new_p = round(n_index*cosd(p_index));
            if (new_n ==0)
                new_n = 1;
            end
            if (new_p ==0)
                new_p = 1;
            end
            new_matrix(new_m,new_n,new_p) = old_matrix (m_index,n_index,p_index);
        end
     end
 end

这是一个缓慢的过程,我有额外的代码来确保所有新索引从1开始。是否有更快的方法来执行此操作,而不是使用for循环遍历每个元素?

谢谢

编辑:我忘了提及,在for循环开始之前初始化new_matrix的大小。

Edit2:修复了代码,使其在matlab中运行。

编辑3:添加图像以演示我希望获得的转换。图像1具有45个平面的起始矩阵。图2显示了view(3)命令的音量。图3显示了new_matrix的m-p侧视图。

Starting matrix as a volume

Transformed matrix as a volume

Side view of new_matrix.

0 个答案:

没有答案