使用其他位置矩阵将1D向量的元素放入3D矩阵

使用其他位置矩阵将1D向量的元素放入3D矩阵

本文介绍了使用其他位置矩阵将1D向量的元素放入3D矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据点向量,这些数据点是由其他程序从3D数组保存在一个大列表中的.向量的长度为nk个元素. nk = nx * ny * nz,其中nx,ny和nz是原始3D阵列的尺寸.

I have a vector of data points which were saved from a 3D array by some other program in one big list. The vector is nk elements long. nk = nx*ny*nz where nx, ny and nz are the dimensions of the original 3D array.

原始数组中数据点的位置存储在(nk x 3)数组中,并按每行(position(k,:))排列,给出相应数据的(i,j,k)位置观点.

The position of a data point in the original array is stored in a (nk x 3) array, arranged with each row (position(k,:)) giving the (i,j,k) position of the corresponding data point.

我不能在数据数组上使用重整形,因为位置矢量并不简单(它取决于某些与数据生成方式有关的东西-它不是完全随机的-但我不一定会知道看起来像之前).

I can't use reshape on my data array as the position vector is not simple (it depends on some stuff to do with how the data was generated - it isn't completely random - but I won't necessarily know what it looks like beforehand).

如果nk向量称为数据",则nk x 3位置数组称为位置",输出数组称为"data_reshaped",那么我目前正在执行以下操作:

If the nk vector is called 'data', the nk x 3 position array is called 'position' and the output array is called 'data_reshaped' then currently I am doing the following:

for k = 1:nk
    data_reshaped(position(k,1),position(k,2),position(k,3)) = data(k);
end

这真的很慢-是否有一些更快速的方法而对位置"是什么样子却不太了解?

This is really slow - is there some faster method without knowing much about what 'position' looks like?

推荐答案

您可以使用 sub2ind 函数:

You could use sub2ind function:

data_reshaped = zeros(nx, ny, nz);
data_reshaped( sub2ind([nx ny nz], position(:,1), ...
                                   position(:,2), ...
                                   position(:,3)) ) = data;

这篇关于使用其他位置矩阵将1D向量的元素放入3D矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 23:38