假设我有以下矩阵,
1 2 3 4 5 6 7 8
2 3 4 5 6 7 8 1
3 4 5 6 7 8 1 2
4 5 6 7 8 1 2 3
1 8 7 6 5 4 3 2
2 7 6 5 4 3 2 9
3 6 5 4 3 2 9 8
4 5 4 3 2 9 8 7
我想创建一个包含4个矩阵的数组,按照#1列分类。
例如,输出应如下所示,
[
2 3 4 5 6 7 8
8 7 6 5 4 3 2
3 4 5 6 7 8 1
7 6 5 4 3 2 9
4 5 6 7 8 1 2
6 5 4 3 2 9 8
5 6 7 8 1 2 3
5 4 3 2 9 8 7
]
我的目标是将this Parzen function应用于每个人。
是否类似以下内容?
function [retval] = bayes (train, test)
classCounts = rows(unique(train(:,1)));
pdfmx = ones(rows(test), classCounts);
variance = 0.25;
pdf = parzen(train(:,2:end), test(:,2:end), variance);
for cl=1:classCounts
clidx = train(:,1) == cl;
mu(:,cl) = train(clidx,2:end);
end
retval = mu;
endfunction
此代码生成以下错误,
>> bayes(mat, mat)
error: bayes: A(I,J,...) = X: dimensions mismatch
error: called from
bayes at line 11 column 12
>>
答案 0 :(得分:2)
这是accumarray
的经典作品。它输出一个单元阵列,但我建议你继续使用3D矩阵。
%// data
A = [1 2 3 4 5 6 7 8
2 3 4 5 6 7 8 1
3 4 5 6 7 8 1 2
4 5 6 7 8 1 2 3
1 8 7 6 5 4 3 2
2 7 6 5 4 3 2 9
3 6 5 4 3 2 9 8
4 5 4 3 2 9 8 7]
%// input
groupid = A(:,1); %// group identifier
rowidx = 1:size(A,1); %// row index
%// accumarray
cellArray = accumarray(groupid(:),rowidx (:),[],@(x) {A(x,2:end)})
%// transform cell array to 3D-Matrix
threeDArray = cat(3,cellArray{:})
accumarray
是什么?
vals = rowidx
的所有元素都使用相同的subs = groupid
,对其进行分组并执行操作。A(x,2:end)
表示您将所有rowidx
与groupid
中存储的x
相同,并使用它来访问您的矩阵A
,您放置{}
1}}来获取单元格数组输出cellArray{1} =
8 7 6 5 4 3 2
2 3 4 5 6 7 8
cellArray{2} =
7 6 5 4 3 2 9
3 4 5 6 7 8 1
cellArray{3} =
6 5 4 3 2 9 8
4 5 6 7 8 1 2
cellArray{4} =
5 4 3 2 9 8 7
5 6 7 8 1 2 3
threeDArray(:,:,1) =
8 7 6 5 4 3 2
2 3 4 5 6 7 8
threeDArray(:,:,2) =
7 6 5 4 3 2 9
3 4 5 6 7 8 1
threeDArray(:,:,3) =
6 5 4 3 2 9 8
4 5 6 7 8 1 2
threeDArray(:,:,4) =
5 4 3 2 9 8 7
5 6 7 8 1 2 3
如果输出中的行顺序很重要,则需要accumarray
的“稳定版本”(受this answer启发:
%// stabilize accumarray
sz = max(groupid,[],1);
[~, I] = sort(groupid*cumprod([1,sz(1:end-1)]).');
%// stable accumarray
cellArray = accumarray(groupid(I,:),rowidx(I),[],@(x) {A(x,2:end)})
cellArray{1} =
2 3 4 5 6 7 8
8 7 6 5 4 3 2
cellArray{2} =
3 4 5 6 7 8 1
7 6 5 4 3 2 9
cellArray{3} =
4 5 6 7 8 1 2
6 5 4 3 2 9 8
cellArray{4} =
5 6 7 8 1 2 3
5 4 3 2 9 8 7
threeDArray(:,:,1) =
2 3 4 5 6 7 8
8 7 6 5 4 3 2
threeDArray(:,:,2) =
3 4 5 6 7 8 1
7 6 5 4 3 2 9
threeDArray(:,:,3) =
4 5 6 7 8 1 2
6 5 4 3 2 9 8
threeDArray(:,:,4) =
5 6 7 8 1 2 3
5 4 3 2 9 8 7