索引矩阵 Matlab

发布于 2024-12-17 05:26:44 字数 678 浏览 0 评论 0原文

我正在尝试计算两个矩阵,其中一个是 400*2,另一个是 20*20。第一个包含 400 个点的 x 和 y 坐标,第二个包含另外 400 个点,其中 x 为 2nd(i,i),y 为 (i,k),i 和 k 都是 20 个数字计数器。我正在尝试计算这两个矩阵点之间的距离会给我 400*400 矩阵。我正在使用的代码是;

for i=1:20
    for j=1:400
        for k=1:20
        L(j,)=sqrt((C(j,1)-M(i,i))^2+(C(j,2)-M(k,i))^2);
        end
    end
end

C 是第一个矩阵,M 是第二个矩阵。现在我知道我让它听起来有点困难,但问题是我找不到一个计数器来给 L(j, ) 那部分。如果你们有任何想法请告诉...

编辑:首先认为有 2 个点向量。 X 为 -0.95:0.1:0.95 和 y 相同。 M 向量是所有 x 与 Y 配对,因此它将生成 20*20 矩阵或 400*2,但是我无法使其像 400*2 一样工作,因此使用 20*20。矩阵有点小我很难做到这一点,我对 M 的索引表明了这一点。如果您有更好的方法来配对它们,我也会很感激。

我的矩阵看起来像这样;

-0.95 -0.85 -0.75...
-0.95 -0.85 -0.75 ... 
.
.
.

I'm trying to compute two matrices , one of which is 400*2 and the other being 20*20. The first one contains x and y coordinates of 400 points and the second being another 400 points with x's being 2nd(i,i) and y's being (i,k) both i and k's are 20 number counters.I'm trying to compute the distances between those 2 matrix points which would give me 400*400 matrix. The code i'm using is ;

for i=1:20
    for j=1:400
        for k=1:20
        L(j,)=sqrt((C(j,1)-M(i,i))^2+(C(j,2)-M(k,i))^2);
        end
    end
end

C being the first matrix and M being the second. Now i know i made it sound a little hard but the trouble is i cant find a counter to give to L(j, ) that part. If you guys have any idea please do tell...

Edit: Well first of all think there is 2 point vectors. X's being -0.95:0.1:0.95 and y's are the same. M vector is that all of the x's paired with a Y so it would make 20*20 matrix or a 400*2 however i could not make it work like a 400*2 so have gone with 20*20.The matrix is a little hard for me to do so my indexing of M shows that.If you got a better way to pair them i would appreciate it as well.

My matrix looks like this ;

-0.95 -0.85 -0.75...
-0.95 -0.85 -0.75 ... 
.
.
.

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

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

发布评论

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

评论(2

じ违心 2024-12-24 05:26:44

上面的代码应该很慢......使用大量的for循环不是“matlab风格”
我要做的是:

Mdiag=repmat(diag(M)',20,1);
L=pdist2(C,[Mdiag(:) M(:)]);

第一行提取 M 的对角项并重复它们。

之后 [Mdiag(:) M(:)] 是第二组点的标准形式。

pdist2 是计算所有成对距离的函数。

The above code should be very slow... Using a lot of for-loop is not "matlab-style"
What I would do is:

Mdiag=repmat(diag(M)',20,1);
L=pdist2(C,[Mdiag(:) M(:)]);

The first line extracts the diagonal terms of M and repeat them.

After that [Mdiag(:) M(:)] is a standard form of your second set of points.

pdist2 is a function that compute all the pairwise distances.

只是偏爱你 2024-12-24 05:26:44

它们的顺序重要吗?我假设不会,只要您知道该顺序是什么。我会用这个:

for i=1:20
    for j=1:400
        for k=1:20
            L(j,(i-1)*20+k) = sqrt((C(j,1)-M(i,i))^2+(C(j,2)-M(k,i))^2);
        end
    end
end

Does it matter what order they are in? I am assuming it doesn't, so long as you know what that order is. I'd use this:

for i=1:20
    for j=1:400
        for k=1:20
            L(j,(i-1)*20+k) = sqrt((C(j,1)-M(i,i))^2+(C(j,2)-M(k,i))^2);
        end
    end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文