1

是否可以重塑矩阵,使得

x1 = 
    1   5
    3   4
    4   3
    7   1

变成

x2 =
    5
    NaN
    4
    3
    NaN
    NaN
    1

反之亦然,其中第一列x1是对应于行# in 的索引x2

4

1 回答 1

5

Create an array with NaNs and fill it with values:

x2          = NaN(max(x1(:,1)),1);
x2(x1(:,1)) = x1(:,2);

Now, if zero padding is acceptable, then you can simply use the second line directly without first creating out.

Alternatively, for your specific example (no overlapping indices) the same result is achieved with:

accumarray(x1(:,1),x1(:,2),[],[],NaN)

Going the other way

idx = ~isnan(x2);
x1  = [find(idx) x2(idx)];
于 2013-04-24T15:39:40.160 回答