用 Octave 语言创建矩阵 A = [4 ... 128]

发布于 2025-01-10 02:08:26 字数 274 浏览 1 评论 0原文

您能帮忙了解如何使用简化方式Octave中创建矩阵吗?

我需要(矩阵) A = [4, 8, 16, 32, 64, 128]; 想要使用类似 A = [4: *2 : 128] (意味着 start = 4,step = *2 : finish = 128),但这在 Octave 中不起作用。

矩阵 B = [1 4 9 16 25 36] 也需要执行同样的操作,其中步骤一开始为 3,下一步增加 2。

有什么想法吗?

Can you help to know how to create a matrix in Octave using a shortened way?

I need to have (matrix) A = [4, 8, 16, 32, 64, 128];
Want to use something like A = [4: *2 : 128] (meaning start = 4, step = *2 : finish = 128), but this doesn't work in Octave.

The same needs to be done to matrix B = [1 4 9 16 25 36], where step is 3 at the beginning and is increasing by 2 on the next step.

Any ideas?

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

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

发布评论

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

评论(2

提笔书几行 2025-01-17 02:08:27

使用冒号运算符,您只能执行相同大小的步骤。但请注意,您的矩阵

A = [4, 8, 16, 32, 64, 128];

具有结构 [2^2, 2^3, 2^4, ..., 2^7],因此您可以利用广播并将其定义为

A = 2.^[2,3,4,5,6,7];

或 简单

A = 2.^(2:7);

With the colon operator you can only do steps of the same size. But notice that your matrix

A = [4, 8, 16, 32, 64, 128];

has the structure [2^2, 2^3, 2^4, ..., 2^7], so you can make use of broadcasting and define it as

A = 2.^[2,3,4,5,6,7];

or simply

A = 2.^(2:7);
歌入人心 2025-01-17 02:08:27

您可以使用循环来完成该任务。您只需要在循环语句中编写一致的规则即可。一种可能的方法如下:

  start=1;
  finish=36;

  matrix(1)=start; i=2; last_term=start; %inicializations needed for the loop start
  while last_term < finish
    matrix(i)=matrix(i-1)+(1+2*(i-1)); %here you define your rule
    last_term=matrix(i);
    i=i+1;
  endwhile
  matrix %your output is printed in the console

You can use a loop for that task. You just need to write a consistent rule in the statments of your loop. A possible way to do that is the following:

  start=1;
  finish=36;

  matrix(1)=start; i=2; last_term=start; %inicializations needed for the loop start
  while last_term < finish
    matrix(i)=matrix(i-1)+(1+2*(i-1)); %here you define your rule
    last_term=matrix(i);
    i=i+1;
  endwhile
  matrix %your output is printed in the console
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文