循环期间矩阵索引超出范围

发布于 2025-01-26 18:35:34 字数 387 浏览 2 评论 0原文

an x n矩阵。

我有此代码:

[m,n] = size(a);
x = zeros(m,1);
for j=1:1:n
    if(j==1)
    a(1,:) = [];
    else
    
    end
    disp(a);
    a(:,j) = [];
    
    disp(x);
    disp(a);      
end

它在行上给出了错误a(:,j)= [];说明

矩阵索引的删除范围不超出范围。

为什么?我不明白,帮助您赞赏。

a is an nxn matrix.

I have this code:

[m,n] = size(a);
x = zeros(m,1);
for j=1:1:n
    if(j==1)
    a(1,:) = [];
    else
    
    end
    disp(a);
    a(:,j) = [];
    
    disp(x);
    disp(a);      
end

And it gives error on the line a(:,j) = []; which says

Matrix index is out of range for deletion.

Why? I dont understand, help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悟红尘 2025-02-02 18:35:34

而不是:

for j=1:1:n

尝试使用:

for j=n:-1:1

发生了什么事是您从j = 1开始删除列,因此每次缩短矩阵。一旦j大于剩下的 A中的列数,就会丢弃该错误。

我建议向后迭代解决这个问题(因为您的索引在随着矩阵尺寸的减小而减小)。

Instead of:

for j=1:1:n

Try using:

for j=n:-1:1

What's going on is you're deleting columns starting from j=1 and therefore shortening your matrix each time. As soon as j is greater than the remaining number of columns in a, it will throw that error.

Iterating backwards as I am suggesting will solve this problem (because your index is decreasing at the same time as your matrix size is decreasing).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文