MATLAB 中的数组旋转

发布于 2024-10-10 06:50:28 字数 256 浏览 2 评论 0原文

在 MATLAB 中,有没有办法将数组的元素旋转到另一个维度,例如:

y=[-1,0,1] -->  y=[-1; 0; 1] (like transpose)

y=[-1,0,1] -->  y(:,:,1)=-1, y(:,:,2)=0, y(:,:,3)=1 

y=[-1,0,1] -->  y(:,:,1,1)=-1, y(:,:,1,2)=0, y(:,:,1,3)=1

我想避免 for 循环。

In MATLAB, is there a way to rotate the elements of a array to another dimension, like:

y=[-1,0,1] -->  y=[-1; 0; 1] (like transpose)

y=[-1,0,1] -->  y(:,:,1)=-1, y(:,:,2)=0, y(:,:,3)=1 

y=[-1,0,1] -->  y(:,:,1,1)=-1, y(:,:,1,2)=0, y(:,:,1,3)=1

I would like to avoid for loops.

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

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

发布评论

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

评论(2

思念满溢 2024-10-17 06:50:28

您可以使用转置(函数RESHAPE,或函数 排列。例如:

y = [-1 0 1];               %# Your 1-by-3 sample array

y2 = y.';                  %'# Transposing y gives you a 3-by-1 array
y2 = reshape(y,[3 1]);      %# This also gives you a 3-by-1 array
y2 = permute(y,[2 1]);      %# This also gives you a 3-by-1 array

y3 = reshape(y,[1 1 3]);    %# This gives you a 1-by-1-by-3 array
y3 = permute(y,[3 1 2]);    %# This also gives you a 1-by-1-by-3 array

y4 = reshape(y,[1 1 1 3]);  %# This gives you a 1-by-1-by-1-by-3 array
y4 = permute(y,[4 1 2 3]);  %# This also gives you a 1-by-1-by-1-by-3 array

You can do these sorts of matrix operations using transposition, the function RESHAPE, or the function PERMUTE. For example:

y = [-1 0 1];               %# Your 1-by-3 sample array

y2 = y.';                  %'# Transposing y gives you a 3-by-1 array
y2 = reshape(y,[3 1]);      %# This also gives you a 3-by-1 array
y2 = permute(y,[2 1]);      %# This also gives you a 3-by-1 array

y3 = reshape(y,[1 1 3]);    %# This gives you a 1-by-1-by-3 array
y3 = permute(y,[3 1 2]);    %# This also gives you a 1-by-1-by-3 array

y4 = reshape(y,[1 1 1 3]);  %# This gives you a 1-by-1-by-1-by-3 array
y4 = permute(y,[4 1 2 3]);  %# This also gives you a 1-by-1-by-1-by-3 array
泡沫很甜 2024-10-17 06:50:28

虽然 reshape 和 permute 是更强大的工具,但您可以使用以下方法轻松解决给定的示例:

y = [-1 0 1];
y2(:,1)=y;
y3(1,1,:)=y;
y4(1,1,1,:)=y;

While reshape and permute are more powerful tools, you can solve the given example quite easy with:

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