首页 > 解决方案 > 重新排列matlab中相关的两个向量

问题描述

我正在使用matlab分析多个电路的效率。输出是最大工作距离和分析的电路功率。该向量的输出如下:

>> display(distance)

distance =

     8    21    21     4     3     8     3     8     2     6    10     7     6     8    12    11     6     8

>>display(power)

power =

  Columns 1 through 13

    3.2047    3.5666    3.7578    1.8184    3.0810    3.7973    2.8699    3.3953    2.5971    3.1933    3.8191    3.7992    3.4802

  Columns 14 through 18

    4.1104    4.0836    3.2191    3.9155    0.2394

如您所见,我的电路 1 的功率为 3.2047,最大距离为 8m。我有电路 6,其最大距离相同,功率为 3.7973。我想重新排列distance矢量以使其成为新月形(例如以 2 开头并以 21 结尾)并能够相应地重新排列power。下面你有display我希望看到这样做来澄清这个问题。

>> display(distanceReorganized)

distanceReorganized =

     2    3    3     4     6     6     6     7     8     8    8     8     8     10    11     12     21    21

>>display(powerReorganized)

powerReorganized =

  Columns 1 through 13

    2.5971    2.8699    3.0810    1.8184    3.1933    3.4802    3.9155    3.7992    0.2394    3.2047    3.3953    3.7973    4.1104

  Columns 14 through 18

    3.8191    3.2191    4.0836    3.5666    3.7578

标签: matlab

解决方案


使用来自的第二个输出参数sort

[~,I] = sort(distance, 'ascend');
distanceReorganized = distance(I);
powerReorganized = power(I);

编辑:根据您的评论,sortrows应该做您正在寻找的东西:

circuitData = table(distance', power', 'VariableNames', {'Distance', 'Power'});
sortrows(circuitData,{'Distance','Power'})

然后,您可以根据需要将表的列重新分配给新的向量变量。

这是sortrows的文档。


推荐阅读