【问题标题】:Create a matrix using elements of other vectors in matlab在matlab中使用其他向量的元素创建一个矩阵
【发布时间】:2012-06-28 07:43:59
【问题描述】:

我有两个向量 a, b

a=[1; 2; 3; 4]
b=[1; 2; 3] 

我想创建一个看起来像这样的矩阵

c=[1 1; 2 1; 3 1; 4 1; 1 2; 2 2; 3 2; 4 2; 1 3; 2 3; 3 3; 4 3]

【问题讨论】:

标签: matlab vector matrix combinations


【解决方案1】:

这是另一种方式!

c = [repmat(a,numel(b),1),sort(repmat(b,numel(a),1))]

【讨论】:

    【解决方案2】:

    我感觉有更好的方法,仍然......

    p1 = repmat(a,[numel(b),1]);
    p2 =  imresize(b,[numel(a)*numel(b) 1],'nearest');
    answer =  [p1 p2];
    

    找到更好的方法:

     [A,B] = meshgrid(a,b);
     answer = [reshape(B,[],1) reshape(A,[],1)];
    

    Chris Taylor 提出了一种更紧凑的方法:

     [A B]=meshgrid(a,b); [B(:) A(:)];
    

    【讨论】:

    • 更好:[A B]=meshgrid(a,b); [B(:) A(:)]
    • 我总是使用ndgrid - 输出顺序更有意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 2014-07-17
    • 1970-01-01
    • 2017-02-27
    • 2014-07-27
    相关资源
    最近更新 更多