gpt4 book ai didi

matlab - 从矩阵中提取正值和负值

转载 作者:行者123 更新时间:2023-12-04 03:37:38 25 4
gpt4 key购买 nike

我有这样的 3D 矩阵

1   5648.00278672228    -46.43159546036
1 5650.38906894239 68.81787768047
1 5649.13081105839 -4867.55961979647
1 5650.53055771227 4868.95936645035
1 5647.95215053492 4866.38095927300
1 5650.21656586142 -2328.64537459950
1 5651.76371933598 7870.19252807406
1 5649.87288540620 -1168.30169414428

我想将第 3 列的正值和负值提取到两个单独的二维矩阵(POS 和 NEG)中,其中第 1 列包含上述矩阵中行的索引,第二列包含值。

结果应该是这样的

POS = 
2 68.81787768047
4 4868.95936645035
5 4866.38095927300
7 7870.19252807406

NEG =  
1 -46.43159546036
3 -4867.55961979647
6 -2328.64537459950
8 -1168.30169414428

最佳答案

您可以使用find 函数和逻辑索引

POS = [find(A(:,3)>0) A(A(:,3)>0,3)]
NEG = [find(A(:,3)<=0) A(A(:,3)<=0,3)]

对于你的输入矩阵是 A

A = [1   5648.00278672228    -46.43159546036
1 5650.38906894239 68.81787768047
1 5649.13081105839 -4867.55961979647
1 5650.53055771227 4868.95936645035
1 5647.95215053492 4866.38095927300
1 5650.21656586142 -2328.64537459950
1 5651.76371933598 7870.19252807406
1 5649.87288540620 -1168.30169414428]

结果将是(使用format shortG)

POS =

2 68.818
4 4869
5 4866.4
7 7870.2


NEG =

1 -46.432
3 -4867.6
6 -2328.6
8 -1168.3

由于@Dennis Jaheruddin 和 Khurram Majeed 的评论,第二个发现已被删除。

关于matlab - 从矩阵中提取正值和负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18760946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com