matlab - 重新排列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
解决方案
使用来自的第二个输出参数sort
。
[~,I] = sort(distance, 'ascend');
distanceReorganized = distance(I);
powerReorganized = power(I);
编辑:根据您的评论,sortrows
应该做您正在寻找的东西:
circuitData = table(distance', power', 'VariableNames', {'Distance', 'Power'});
sortrows(circuitData,{'Distance','Power'})
然后,您可以根据需要将表的列重新分配给新的向量变量。
这是sortrows的文档。
推荐阅读
- pytorch - 梯度计算
- algorithm - 找到最大的方形子矩阵在具有值 1 和 0 的 Matrix 中具有值 1
- c# - 如何退出“支撑范围”?
- python-3.x - 无法从 Windows 10 中的 python 3 anaconda python 中的集合导入计数器
- angular - 在启动时决定为通过依赖注入提供的服务使用哪个实现
- python - python pandas - 转换表
- c# - 编译时有多个版本的 Newtonsoft.json
- doctrine - 如何从我的 queryBuilder 中限制某些实体?
- php - 调用类方法的公共构造函数和调用另一个类方法的类方法有什么区别?
- c# - 如何在 Unity 中调用 Restart 方法?