元素x的MATLAB矢量(i)或y(i)与最大值(ABS(x)中,ABS(y))为 [英] MATLAB Vector of elements x(i) or y(i) with max(abs(x),abs(y))
问题描述
很抱歉的称号,也想不出一个简洁的方式表达出该问题。我需要编写一个MATLAB单行,让你的元素Z(I)其中,z(i)为元素x马克斯(ABS(X(I)提供:(i)或Y(I))的载体,ABS (义)))。即,z是其元素具有最大绝对值x或y的第i个元素的向量。
Sorry for the title, couldn't think of a concise way to phrase the problem. I need to write a MATLAB one-liner that gives you a vector of elements z(i) where z(i) is the element x(i) or y(i) given by max(abs(x(i)),abs(y(i))). I.e, z is the vector whose elements are the ith elements of x or y which has the maximum absolute value.
我
max(abs(x),abs(y))
不过,这显然给您最大的绝对值的向量。这是接近我想要的,但我需要得到原始载体的签回。我不知道如何做到这一点在一行。
But this obviously gives you a vector of the greatest absolute values. This is close to what I want, but I need to get the sign back of the original vector. I'm not sure how to do this on a single line.
推荐答案
根据这一假设 X
和是
是数组(不一定向量)相同的尺寸,你可以使用逻辑索引:
Under the assumption that x
and y
are arrays (not necessarily vectors) of identical dimensions, you can use logical indexing:
(abs(x)>=abs(y)).*x + (abs(x)<abs(y)).*y
有关信息, ABS(X)&GT; = ABS(Y)
是其中一个逻辑阵列,所有有效索引, K
个分量是
For information, abs(x)>=abs(y)
is a logical array for which, for all valid indices, the k
th component is
-
1
如果X(k)的
大于或等于ÿ(K )
, -
0
其他。
1
ifx(k)
is greater than or equal toy(k)
,0
otherwise.
例如:
>> x = [4;7;-1;9;6];
>> y = [5;2;-3;9;3];
>> (abs(x)>=abs(y)).*x + (abs(x)<abs(y)).*y
ans =
5
7
-3
9
6
这篇关于元素x的MATLAB矢量(i)或y(i)与最大值(ABS(x)中,ABS(y))为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!