为了巧妙地使用线性索引accumarray,我有时觉得需要根据游程编码生成序列.由于没有内置函数,我要求最有效的方法来解码在RLE中编码的序列.
为了使这个公平比较,我想为该功能设置一些规范:
values指定了相同长度的可选第二个参数,则输出应该根据这些值,否则只是值1:length(runLengths).runLengthsvalues 是一个单元阵列.runLengths 简而言之:该函数应该等效于以下代码:
function V = runLengthDecode(runLengths, values)
[~,V] = histc(1:sum(runLengths), cumsum([1,runLengths(:).']));
if nargin>1
V = reshape(values(V), 1, []);
end
V = shiftdim(V, ~isrow(runLengths));
end
Run Code Online (Sandbox Code Playgroud)
以下是一些测试用例
runLengthDecode([0,1,0,2])
runLengthDecode([0,1,0,4], [1,2,4,5].')
runLengthDecode([0,1,0,2].', [10,20,30,40])
runLengthDecode([0,3,1,0], {'a','b',1,2})
Run Code Online (Sandbox Code Playgroud)
和他们的输出:
>> runLengthDecode([0,1,0,2])
ans =
2 4 4
>> runLengthDecode([0,1,0,4], [1,2,4,5].')
ans =
2 5 5 5 5
>> runLengthDecode([0,1,0,2].', [10,20,30,40])
ans =
20
40
40
>> runLengthDecode([0,3,1,0],{'a','b',1,2})
ans …Run Code Online (Sandbox Code Playgroud) 我有一个大小为RGB的图像uint8(576,720,3),我想将每个像素分类为一组颜色.我已经使用rgb2labRGB 转换为LAB空间,然后删除了L层,因此它现在double(576,720,2)由AB组成.
现在,我想将其归类为我在另一幅图像上训练的一些颜色,并将它们各自的AB表示计算为:
Cluster 1: -17.7903 -13.1170
Cluster 2: -30.1957 40.3520
Cluster 3: -4.4608 47.2543
Cluster 4: 46.3738 36.5225
Cluster 5: 43.3134 -17.6443
Cluster 6: -0.9003 1.4042
Cluster 7: 7.3884 11.5584
Run Code Online (Sandbox Code Playgroud)
现在,为了将每个像素分类/标记到簇1-7,我目前执行以下操作(伪代码):
clusters;
for each x
for each y
ab = im(x,y,2:3);
dist = norm(ab - clusters); // norm of dist between ab and each cluster
[~, idx] = min(dist);
end
end
Run Code Online (Sandbox Code Playgroud)
然而,由于图像分辨率和我手动遍历每个x和y,这非常慢(52秒).
是否有一些我可以使用的内置函数执行相同的工作?必须有.
总结一下:我需要一种分类方法,将像素图像分类为已定义的一组聚类.
performance matlab classification machine-learning data-mining