以行方式重塑MATLAB向量 [英] Reshape MATLAB vector in Row-wise manner

查看:67
本文介绍了以行方式重塑MATLAB向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个矩阵a = [1 2 3 4 5 6];,例如,如何以行方式重塑它 reshape(a, 2, 3)产生

Say I have a matrix a = [1 2 3 4 5 6];, how do I reshape it in a row-wise manner for example reshape(a, 2, 3) to yield

1 2 3 
4 5 6

而不是MATLAB产生的默认列式结果:

rather than the default column-wise result produced by MATLAB of:

1 3 5 
2 4 6

我认为这是一项微不足道的任务,可能具有内置功能来实现.我已经实现了一个可以完美实现此功能的函数...但是,有没有一种更短,更整洁,更MATLAB的方式?谢谢.

I believe this is a trivial task which probably has an inbuilt function to achieve this. I have already implemented a function that does this perfectly... however, is there a shorter, neater and more MATLAB way? Thanks.

function y = reshape2(x, m, n) 
  y = zeros(m, n);

  ix = 0; 
  for i = 1:m
     for j = 1:n
         ix = ix + 1;
         y(i, j) = x(ix);
     end 
  end 
end

推荐答案

如何?

reshape(a, 3, 2)'

这篇关于以行方式重塑MATLAB向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆