4

Matlab の一連のカテゴリに従って値のベクトルをグループ化する簡単な (理想的には複数の for ループなしで) 方法はありますか?

私はフォームにデータマトリックスを持っています

CATEG_A    CATEG_B   CATEG_C  ...   VALUE

   1          1        1      ...   0.64
   1          2        1      ...   0.86
   1          1        1      ...   0.74
   1          1        2      ...   0.56
  ...

私が欲しいのはN次元配列です

 all_VALUE( CATEG_A, CATEG_B, CATEG_C, ..., index ) = VALUE_i

もちろん、同じカテゴリの組み合わせを持つ値はいくつでもある可能性があるためsize(end)、最大のカテゴリの値の数になります。残りの項目は で埋められnanます。

あるいは、私は満足しています

 all_VALUE { CATEG_A, CATEG_B, CATEG_C, ... } ( index )

つまり、ベクトルのセル配列です。ピボット テーブルの作成に少し似ていると思いますが、n 次元を使用し、mean.

ヘルプでこの機能を見つけました

A = accumarray(subs,val,[],@(x) {x})

しかし、私はそれを自分のやりたいようにする方法を理解できませんでした!

4

2 に答える 2

2

これも混乱ですが、機能します。それはNDアレイのやり方です。

X = [1        1        1        0.64
     1        2        1        0.86
     1        1        1        0.74
     1        1        2        0.56]; %// data
N = size(X,1); %// number of values
[~, ~, label] = unique(X(:,1:end-1),'rows'); %// unique labels for indices
cumLabel = cumsum(sparse(1:N, label, 1),1); %// used for generating a cumulative count
    %// for each label. The trick here is to separate each label in a different column
lastInd = full(cumLabel((1:N).'+(label-1)*N)); %'// pick appropriate values from 
    %// cumLabel to generate the cumulative count, which will be used as last index
    %// for the result array
sizeY = [max(X(:,1:end-1),[],1) max(lastInd)]; %// size of result
Y = NaN(sizeY); %// initiallize result with NaNs
ind = mat2cell([X(:,1:end-1) lastInd], ones(1,N)); %// needed for comma-separated list
Y(sub2ind(sizeY, ind{:})) = X(:,end); %// linear indexing of values into Y

あなたの例の結果は、次の 4D 配列です。

>> Y
Y(:,:,1,1) =
    0.6400    0.8600
Y(:,:,2,1) =
    0.5600       NaN
Y(:,:,1,2) =
    0.7400       NaN
Y(:,:,2,2) =
   NaN   NaN
于 2015-02-11T15:10:52.033 に答える