2016-06-28 68 views
0

A有兩個矩陣<1x100 double>即I plot(A,B)。在矩陣B的一些列中存在NaN。我想要做的是在A和B都存在的A和B之間找出差異(比如A高於B)。我希望它能夠以當地最大值出現,只需將其打印出來即可。disp。例如:當max(A)= 20和B = 10時,我想顯示:A比B多10。然後當A再次變爲30時,我想顯示:max(A)是20以上B.當B不是NaN時A和B之間的差異

非常感謝幫助!

+0

使用'nanmax(B)'或'max(B(〜isnan(B)))''。 –

+0

我覺得你發佈了兩次,所以請考慮關閉舊的。 – Finn

回答

1
ValidData = ~isnan(A) & ~isnan(B); % Get all indices where both exist 
plot(A(ValidData),B(ValidData)); 

isnan給你這是NaN所有元素,這否定(~),你會得到所有非NaN值。這可以用作AB的邏輯索引。

爲了獲得最大的價值,請執行AndrasDeak suggests

nanmax(A-B) 

更新:顯然max現在已經作爲默認忽略NaN值,這樣你就可以直接使用:

[MaxValue,MaxPosition] = max(A-B); 
0

我不確定你想忽略nan並將向量剪切成數字,或者忽略其中一個值爲nan的部分。對於後一種情況:

%show you the difference 
C=A-B 
% ~ means you ignore that output, and the second output of max is the postion 
[~,position]=max(A-B); 
disp(['At Position ' num2str(position) ' A is ' num2str(A(position)) ' and ' num2str(A(position)-B(position)) ' bigger than B'])