从包含每个可能对的最大值的向量构建一个矩阵

发布于 2025-01-07 23:30:47 字数 445 浏览 0 评论 0原文

给定一个大小为 n 的向量,

A=[2 2 5 1] % n=4

构建一个大小为 nxn 的矩阵 R,其中与元素 (i,j) 对应的值是 A(i) 和 A(j) 之间的最大值,

R = 
    2     2     5     2
    2     2     5     2
    5     5     5     5
    2     2     5     1 

我使用 for 循环来执行此操作。有更有效的方法吗?

 R = zeros(size(a,2))
 for i=1:size(R,1)
    for j=1:size(R,2)
        R(i,j) = max(A(i),A(j));
    end
 end

感谢您的帮助 :)

Given a vector of size n

A=[2 2 5 1] % n=4

Build a matrix R of size nxn in which the value correspondent to the element (i,j) is the maximum between A(i) and A(j)

R = 
    2     2     5     2
    2     2     5     2
    5     5     5     5
    2     2     5     1 

I am doing this with a for loop. Is there a more efficient way?

 R = zeros(size(a,2))
 for i=1:size(R,1)
    for j=1:size(R,2)
        R(i,j) = max(A(i),A(j));
    end
 end

Thanks for your help :)

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

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

发布评论

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

评论(4

剩一世无双 2025-01-14 23:30:47

这至少是一种更Matlaby的方式:

B=ones(size(A))'*A
R=max(B,B')

我希望它会快得多。

This is at least a more matlaby way:

B=ones(size(A))'*A
R=max(B,B')

I would expect it's a lot faster.

ゝ杯具 2025-01-14 23:30:47

嗯,说实话,真正的 MATLAB 方法并不是 Johan 所建议的那样。这甚至不是最快的方法。使用 bsxfun 的速度要快三倍,以真正的 MATLAB 方式。

>> A= rand(1,1000);

>> tic,B=ones(size(A))'*A;R=max(B,B');toc
Elapsed time is 0.027081 seconds.

>> tic,R = bsxfun(@max,A',A);toc
Elapsed time is 0.006306 seconds.

Well, to be honest, the true MATLAB way is not what Johan has suggested. It is not even the fastest way. Three times faster is to use bsxfun, in the true MATLAB way.

>> A= rand(1,1000);

>> tic,B=ones(size(A))'*A;R=max(B,B');toc
Elapsed time is 0.027081 seconds.

>> tic,R = bsxfun(@max,A',A);toc
Elapsed time is 0.006306 seconds.
不如归去 2025-01-14 23:30:47

这是一个扩展的评论,而不是一个答案......我失去了与开发服务器的连接,所以认为浪费一些时间是合适的。我定义了一个包含以下代码的脚本文件:

A=1:1500; % bigger vector for stable timing
tStart = tic;
R = zeros(size(A,2));
 for i=1:size(R,1)
    for j=1:size(R,2)
        R(i,j) = max(A(i),A(j));
    end
end
tElapsed = toc(tStart)

另一个脚本包含:

A=1:1500;
tStart = tic;
B=ones(size(A))'*A;
R=max(B,B');
tElapsed = toc(tStart)

以及第三个脚本包含:

A=1:1500;
tStart = tic;
r=size(A,2);
R=ones(r);
for i=1:r
   for j=(i+1):r
       R(i,j)=max(A(i),A(j));
       R(j,i)=R(i,j);
   end
R(i,i)=A(i);
end
tElapsed = toc(tStart)

并各运行 5 次。平均 tElapseds 为:1.7674s、0.0520s、0.0645s,因此 @Johan Lundberg 的答案赢得了(时间)效率的桂冠。

为了完整起见,我计时了@woodchips 的回答;平均经过时间为 0.0206 秒。 @Johan 因荣誉被夺走而蒙受耻辱。

This is an extended comment, not an answer ... I've lost connectivity to my development servers, so thought it appropriate to waste some time. I defined a script file containing the code:

A=1:1500; % bigger vector for stable timing
tStart = tic;
R = zeros(size(A,2));
 for i=1:size(R,1)
    for j=1:size(R,2)
        R(i,j) = max(A(i),A(j));
    end
end
tElapsed = toc(tStart)

another script containing:

A=1:1500;
tStart = tic;
B=ones(size(A))'*A;
R=max(B,B');
tElapsed = toc(tStart)

and a third containing:

A=1:1500;
tStart = tic;
r=size(A,2);
R=ones(r);
for i=1:r
   for j=(i+1):r
       R(i,j)=max(A(i),A(j));
       R(j,i)=R(i,j);
   end
R(i,i)=A(i);
end
tElapsed = toc(tStart)

and ran them each 5 times. The mean tElapseds were: 1.7674s, 0.0520s, 0.0645s, so @Johan Lundberg's answer takes the laurels for (time) efficiency.

For the sake of completeness, I timed @woodchips answer; mean time elapsed was 0.0206s. @Johan suffers the ignominy of having the laurels snatched from his brow.

软糖 2025-01-14 23:30:47
  1. 您的矩阵始终是对称的,因为 max {A(i), A(j)}= max {A(j),A(i)}。这样你就可以将支票减少一半。
  2. 您的对角线始终是您的输入向量。 diag(R) = A

所以我建议你只检查一次:(

r=size(A,2)
R=ones(r)
for i=1:r
   for j=(i+1):r
       R(i,j)=max(A(i),A(j))
       R(j,i)=R(i,j)
   end
R(i,i)=A(i)
end

保存你的尺寸(A,2),因为它在输入后不会改变,否则matlab每次都会计算它!)

  1. Your matrix is always symmetric, since max {A(i), A(j)}= max {A(j),A(i)}. So you can reduce the checks by half.
  2. Your diagonal is always your input vector. diag(R) = A

So I suggest you check only once:

r=size(A,2)
R=ones(r)
for i=1:r
   for j=(i+1):r
       R(i,j)=max(A(i),A(j))
       R(j,i)=R(i,j)
   end
R(i,i)=A(i)
end

(save your size(A,2) since it won't change after input and otherwise matlab will compute it every time!)

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