在matlab中用纳米元素重塑矩阵

时间:2015-02-16 15:22:32

标签: matlab matrix

我在matlab中有一个Nx3 - 矩阵,其中第一列的度数值为0到360,第二列的半径值为0到0.5,第三列的半径值为0。 (M(n,1),M(n,2))中的每个组合都是唯一的,M矩阵和n一个介于1和N之间的随机数,但M(:,1)或{中可能存在值{1}}不止一次。 M(:,2)排序,首先在第一行之后,然后在第二行之后。

我的目标现在是将此矩阵重新整形为M矩阵,其360xV中的唯一值数量为V。如果M(:,2)位置M(:,3)M(o,1)位置M(p,2),则值应位于1 <= o, p <= N位置,如果没有值,那么应该放置一个(o,p) - 值。

有一种简单的方法可以做到这一点,还是我必须为此编写自己的函数?

编辑:
期望的输入:

NaN

期望的输出:

0 0.25 1  
0 0.43 4  
1 0.25 2  
2 0.03 5  
2 0.43 2 

2 个答案:

答案 0 :(得分:2)

您可以使用为输入数组中的unique indicesfirst列查找second的方法,然后使用这些方法在中正确设置元素 (在代码中作为注释更详细地讨论)使用third列中的元素调整输出数组。这是实施 -

%// Input array
A = [
    0 0.25 1
    0 0.43 4
    1 0.25 2
    2 0.03 5
    2 0.43 2 ]

%// Find unique indices for col-1,2
[~,~,idx1] = unique(A(:,1)) %// these would form the row indices of output
[~,~,idx2] = unique(A(:,2)) %// these would form the col indices of output

%// Decide the size of output array based on the "extents" of those indices
nrows = max(idx1)
ncols = max(idx2)

%// Initialize output array with NaNs
out = NaN(nrows,ncols)

%// Set the elements in output indexed by those indices to values from
%// col-3 of input array
out((idx2-1)*nrows + idx1) = A(:,3)

代码运行 -

out =
   NaN     1     4
   NaN     2   NaN
     5   NaN     2

答案 1 :(得分:-1)

  

有一种简单的方法可以做到这一点,还是我必须为此编写自己的函数?

您需要编写一种方法,看到您所描述的内容完全针对您的问题。有找到唯一值的方法,因此在设计for循环时这会有所帮助。

相关问题