在 MATLAB 中,什么命令可以擦除矩阵中除前三列之外的所有列?

发布于 2024-09-12 21:21:51 字数 81 浏览 4 评论 0原文

我本质上是试图获取一个矩阵 a,并将其转换为一个矩阵 af,该矩阵的值位于 a 的前三列中。我只想将 a 修剪为仅前三列。

I am essentially trying to take a matrix, a, and turn it into a matrix af which has the values in the first three columns of a. I just want to prune a down to only its first three columns.

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

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

发布评论

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

评论(3

梦屿孤独相伴 2024-09-19 21:21:51
newMatrix = oldMatrix(:,1:3)

这表示“我有一个名为 oldMatrix 的矩阵。我想将该矩阵的子集存储到 newMatrix 中”,括号中的尺寸表示您想要的子集。第一个冒号表示“所有行”,1:3 表示“第 1 列到第 3 列”。

newMatrix = oldMatrix(:,1:3)

This says "I have a matrix called oldMatrix. I want to store a subset of that matrix into newMatrix" and the dimensions in the parentheses say what subset you want. The first colon signifies "all rows" and the 1:3 signifies "columns 1 through 3".

毁梦 2024-09-19 21:21:51
a = ones(10,10);
a = a(:,1:3);
a = ones(10,10);
a = a(:,1:3);
能怎样 2024-09-19 21:21:51

如果您想删除除前三列之外的所有列,则执行此操作...

A(:,4:end) = [];

如果您只想将前三列复制到新数组中,那么这是正确的...

B = A(:,1:3);

当然,您始终可以这样做基本上删除最后一列...

A = A(:,1:3);

一个比另一个更好吗?似乎没有什么区别。

A = rand(4000);
tic,A(:,4:end) = [];toc
Elapsed time is 0.044124 seconds.

A = rand(4000);
tic,A = A(:,1:3);toc
Elapsed time is 0.040166 seconds.

似乎存在很小的(可重复的)差异,但我并不完全相信它是一致的。 MATLAB 的下一个版本可能会改变这些时间。

If you wish to erase all but the first three columns, then do this...

A(:,4:end) = [];

If you wish to copy over only the first three columns into a new array, then this is right...

B = A(:,1:3);

Of course, you can always do this to essentially erase those last columns...

A = A(:,1:3);

Is one better than the other? There would seem to be little difference.

A = rand(4000);
tic,A(:,4:end) = [];toc
Elapsed time is 0.044124 seconds.

A = rand(4000);
tic,A = A(:,1:3);toc
Elapsed time is 0.040166 seconds.

There seems to be a small (repeatable) difference, but I'd not totally trust that to be consistent. The next release of MATLAB might change those times.

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