0
votes

I executed this code using Feature Matrix 517*11 and Label Matrix 517*1. But once the dimensions of matrices change the code cant be run. How can I fix this?

The error is:

Subscripted assignment dimension mismatch. in this line : edges(k,j) = quantlevels(a);

Here is my code:

function [features,weights] = MI(features,labels,Q)
if nargin <3
    Q = 12;
end

edges = zeros(size(features,2),Q+1);

for k = 1:size(features,2)

    minval = min(features(:,k));
    maxval = max(features(:,k));
    if minval==maxval
        continue;
    end

    quantlevels = minval:(maxval-minval)/500:maxval;

    N = histc(features(:,k),quantlevels);

    totsamples = size(features,1);

    N_cum = cumsum(N);

    edges(k,1) = -Inf;

    stepsize = totsamples/Q;

    for j = 1:Q-1
        a = find(N_cum > j.*stepsize,1);
        edges(k,j) = quantlevels(a);
    end

    edges(k,j+2) = Inf;
end

S = zeros(size(features));
for k = 1:size(S,2)
    S(:,k) = quantize(features(:,k),edges(k,:))+1;   
end


I = zeros(size(features,2),1);
for k = 1:size(features,2)   
    I(k) = computeMI(S(:,k),labels,0);
end


[weights,features] = sort(I,'descend');

%% EOF


function [I,M,SP] = computeMI(seq1,seq2,lag)

if nargin <3
    lag = 0;
end

if(length(seq1) ~= length(seq2))
    error('Input sequences are of different length');
end

lambda1 = max(seq1);
symbol_count1 = zeros(lambda1,1);

for k = 1:lambda1
    symbol_count1(k) = sum(seq1 == k);
end

symbol_prob1 = symbol_count1./sum(symbol_count1)+0.000001;

lambda2 = max(seq2);
symbol_count2 = zeros(lambda2,1);

for k = 1:lambda2
    symbol_count2(k) = sum(seq2 == k);
end

symbol_prob2 = symbol_count2./sum(symbol_count2)+0.000001;


M = zeros(lambda1,lambda2);
if(lag > 0)
    for k = 1:length(seq1)-lag
        loc1 = seq1(k);

        loc2 = seq2(k+lag);

        M(loc1,loc2) = M(loc1,loc2)+1;
    end
else
    for k = abs(lag)+1:length(seq1)
        loc1 = seq1(k);

        loc2 = seq2(k+lag);

        M(loc1,loc2) = M(loc1,loc2)+1;
    end
end

SP = symbol_prob1*symbol_prob2';


M = M./sum(M(:))+0.000001;

I = sum(sum(M.*log2(M./SP)));

function y = quantize(x, q)
x = x(:);
nx = length(x);
nq = length(q);
y = sum(repmat(x,1,nq)>repmat(q,nx,1),2);
1

1 Answers

1
votes

I've run the function several times without getting any error. I've used as input for "seq1" and "seq2" arrays such as 1:10 and 11:20

Possible error might rise in the loops

for k = 1:lambda1
   symbol_count1(k) = sum(seq1 == k);
end

if "seq1" and "seq2" are defined as matrices since sum will return an array while symbol_count1(k) is expected to be single value.

Another possible error might rise if seq1 and seq2 are not of type integer since they are used as indexes in

M(loc1,loc2) = M(loc1,loc2)+1;

Hope this helps.