简体   繁体   English

Matlab中的下标分配维不匹配

[英]Subscripted assignment dimension mismatch in matlab

I executed this code using Feature Matrix 517*11 and Label Matrix 517*1. 我使用功能矩阵517 * 11和标签矩阵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); 在这一行中: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);

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 我已将“ seq1 ”和“ seq2 ”数组(例如1:1011: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. 如果将“ seq1 ”和“ seq2 ”定义为矩阵,因为sum会返回一个数组,而symbol_count1(k)应该是单个值。

Another possible error might rise if seq1 and seq2 are not of type integer since they are used as indexes in 如果seq1seq2不是整数类型,则可能会出现另一个可能的错误,因为它们被用作索引

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM