根据索引和值从数组元素创建结构

时间:2019-04-11 17:26:54

标签: matlab

是否有一种更优雅的方式来表达以下代码(例如,没有显式的for循环)?

P = [0.1 0.2 0.3 0.4];
% pre-allocate symbols array of struct
symbols = repmat(struct('probability', 0, 'indices', []), length(P), 1);
for i =1:length(P)
   symbols(i) = struct('probability', P(i), 'indices', i); 
end

P.S .:如果有人感兴趣,我正在使用这些符号来实现霍夫曼编码。

编辑:受其中一项评论的启发,我可能最终会这样做

P = [0.1 0.2 0.3 0.4];
symbols = [
    [0.1 1];
    [0.2 2];
    [0.3 3];
    [0.4 4];
];
% access probability:
symbols(i)(1)
% access indices:
symbols(i)(2:end)

所以

symbols = [P(:) (1:length(P))']

Edit2 :为了完整起见,这是我正在使用的完整代码(霍夫曼代码)

function [c,h,w]=huffman(P)

assert(abs(sum(P) - 1) < 10e-6, "Probabilities must sum up to 100%");

% compute entropy
h = sum(P .* (-log2(P)));
% each row corresponds to the probability in P
c = cell(length(P), 1); % codes are represent as numerical vectors for bits

P = sort(P, 'descend');
% Preallocate 'symbols' for each probability
% A symbol is used to represent dummy "fused" probabilities as well
% size(symbols) == 1xlength(P) initially
% IMPORTANT: sort P first descending
symbols = struct('probability', num2cell(P), 'indices', num2cell(1:length(P)));
%symbols = repmat(struct('probability', 0, 'indices', []), length(P), 1);
%for i =1:length(P)
%   symbols(i) = struct('probability', P(i), 'indices', i); 
%end

while length(symbols) > 1
    % select the two lowest probabilities and add them
    % O(n) insert worst case vs log(n) binary search...
    last = symbols(end);
    preLast = symbols(end-1);
    % Build the code words by prepending bits
    c(last.indices) = cellfun(@(x)[0 x], c(last.indices), 'UniformOutput', false);
    c(preLast.indices) = cellfun(@(x)[1 x], c(preLast.indices), 'UniformOutput', false);
    % Insert dummy symbol representing combined probability of the two
    % lowest probabilities
    probSum = last.probability + preLast.probability;
    newSymbol = struct('probability', probSum, 'indices', [last.indices preLast.indices]);
    pos = find([symbols.probability] < probSum, 1);
    % insert dummy symbol and remove the two symbols which belong to it
    symbols = [symbols(1:pos-1) newSymbol symbols(pos:end-2)];
end
assert(length(symbols) == 1 && abs(symbols(1).probability - 1) < 10e-6, "Probability of tree root must add up to 100%");
% compute average codeword length
w = sum(cellfun('length', c) .* P(:));

我认为使用数字数组而不是结构并将0存储为“无索引”是更多的工作,因为我必须确保所有索引数组都正确地填充了零,并在使用它们之前调用find(indices> 0)。所以我现在暂时跳过。

这大约是我在互联网上发现的某些random code的三倍,所以这并不可怕。

编辑3:事实上,它比Communication Systems Toolbox中的huffmandict快40%,所以可以。我丢失了一些东西,或者他们不在乎性能。

1 个答案:

答案 0 :(得分:3)

怎么样:

symbols = struct('probability', num2cell(P), 'indices', num2cell(1:length(P)));

或(仅八度音,而不是MATLAB):

symbols = repmat(struct('probability', 0, 'indices', []), length(P), 1);
[symbols.probability] = num2cell(P){:};
[symbols.indices] = num2cell(1:length(P)){:};