简体   繁体   English

MATLAB索引超出矩阵尺寸

[英]MATLAB Index exceeds matrix dimensions

So this is code from a book I am looking at done in Matlab. 这是我正在Matlab中看的一本书的代码。 The model array M is initialized, Q is the norm of the difference of the input vector X and the best-matching model. 初始化模型数组M,Q为输入向量X与最佳匹配模型之差的范数。

M = rand(64,2); % initialization of a 64-model SOM array

Q = zeros(64,1); % quantization error
for t = 1:1000000
    X = rand(1,2); % training input
    % Winner search
        for i = 1:64
            Q(i,1) = norm(X(t,:) - M(i,:));
        end
    [C,c] = min(Q);
end

I get an error Index exceeds matrix dimensions. 我收到一个错误索引超过矩阵尺寸。

Error in som1 (line 8)
            Q(i,1) = norm(X(t,:) - M(i,:));

I can see (or think) the error is coming from the indexing of M, but I am not sure why or how I can fix it. 我可以看到(或认为)错误来自M的索引,但是我不确定为什么或如何解决它。 Any ideas or guidance would be much appreciated! 任何想法或指导将不胜感激!

Let's look for your winner by chucking the innermost loop with bsxfun - 让我们来看看你的获胜者由夹紧与最内层循环bsxfun -

for t = 1:100
    X = rand(1,2); % training input
    [C,c] = min(sum(bsxfun(@minus,X,M).^2,2));
end

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

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