Create multiple column vectors by entering a matrix?

7 ビュー (過去 30 日間)
tmac28
tmac28 2015 年 9 月 28 日
コメント済み: tmac28 2015 年 9 月 30 日
I find myself copy/pasting 3xN matrices into Matlab frequently. When I do paste in the matrix, I eventually like to specify the columns as new variables so that I can use the plot3 command or similar. My current process is as follows:
data = [1 2 3; 1 2 3; 1 2 3];
x = data(:,1);
y = data(:,2);
z = data(:,3);
plot3(x,y,z);
Alternatively, after I paste in data, I plot using the following syntax:
plot3(data(:,1),data(:,2),data(:,3));
I would much prefer a solution such as:
[x,y,z] = [1 2 3; 1 2 3; 1 2 3];
But I know that this does not work. Does anyone have a trick or tip for me to expedite my process?
Thanks!

採用された回答

Kelly Kearney
Kelly Kearney 2015 年 9 月 28 日
data = [1 2 3; 1 2 3; 1 2 3];
data = num2cell(data,1);
[x,y,z] = deal(data{:})
It would be nice if we could somehow combine lines 2 and 3 (i.e. [x,y,z] = deal(num2cell(data,1){:})), but I've never found a neat syntax to do comma separated list expansion in that way.
  1 件のコメント
tmac28
tmac28 2015 年 9 月 30 日
If only...thanks for the tip, though. Works like a charm.

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
Stephen23 2015 年 9 月 29 日
編集済み: Stephen23 2015 年 9 月 29 日
Here is an alternative solution. Rather than littering your workspace with unnecessary variables and wasting memory simply create an anonymous function wrapper for plot3:
myplot3 = @(X,varargin) plot3(X(:,1),X(:,2),X(:,3),varargin{:});
This creates no new data variables in your workspace, and gives neater code:
data = [3,2,1;2,0,2;1,2,3];
myplot3(data)
  1 件のコメント
tmac28
tmac28 2015 年 9 月 30 日
I wish I only needed this for plotting purposes, but you taught me something about varargin, so I appreciate that!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by