HOW TO COMPARE TWO ARRAYS ELEMENTS IN MATLAB

47 ビュー (過去 30 日間)
Micky
Micky 2013 年 2 月 18 日
I had an array AA = [ 0.1, 0.2, 0.3, 0.2, 0.1, -0.1, -0.2, -0.3]. Then I got two arrays one shifted left one element and the other shifted right one element and padded with NaN accordingly. Example: shifted versions of arrays aa = [0.2, 0.3, 0.2, 0.1, -0.1, -0.2, -0.3, NaN] and second array as bb = [NaN, 0.1, 0.2, 0.3, 0.2, 0.1, -0.1, -0.2].
I want to compare these arrays and put the result in another array of same size,such that during comparison if the compared elements have the same sign (-/- or +/+) then the new resulted array should get NaN for that comparison entry and if the signs of the compared elements is different(-/+ or +/-), then keep the original value of the original array from where I started i.e AA.
Something like this, after comparing sign of each element of aa and bb, I should get something like this:
result = [ NaN, NaN, NaN, NaN, 0.1, -0.1, NaN, NaN]
I got the shifted versions, I just want to get some idea about comparison described above.
  1 件のコメント
Cedric
Cedric 2013 年 2 月 18 日
編集済み: Cedric 2013 年 2 月 18 日
Do you always have this type of data and a +/- 1 shift? If so, you don't need to compare vectors to find the location of NaNs and non-NaN elements.
Also, what is the purpose of this approach?

サインインしてコメントする。

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 18 日
aa = [0.2, 0.3, 0.2, 0.1, -0.1, -0.2, -0.3, nan]
bb = [nan, 0.1, 0.2, 0.3, 0.2, 0.1, -0.1, -0.2]
idx=arrayfun(@sign ,aa.*bb)
AA(find(idx==1 | isnan(idx)))=nan
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 19 日
Thanks, but no need to arrayfun
idx=sign(aa.*bb)
AA(find(idx==1 | isnan(idx)))=nan
Micky
Micky 2013 年 2 月 20 日
Thanks Azzi

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2013 年 2 月 18 日
編集済み: Image Analyst 2013 年 2 月 18 日
Here's one way. Not really compact but at least it's explicit and easy to follow:
AA = [0.1, 0.2, 0.3, 0.2, 0.1, -0.1, -0.2, -0.3]
aa = [0.2, 0.3, 0.2, 0.1, -0.1, -0.2, -0.3, NaN]
bb = [NaN, 0.1, 0.2, 0.3, 0.2, 0.1, -0.1, -0.2]
signAA = sign(AA)
% Process aa
signaa = sign(aa)
nanLocations = isnan(aa)
differentSignsaa = signAA ~= signaa
differentSignsaa(nanLocations) = false
% Process bb
signbb = sign(bb)
nanLocations = isnan(bb)
differentSignsbb = signAA ~= signbb
differentSignsbb(nanLocations) = false
% Create result
result= nan(1, length(AA))
% Transfer values.
indexesToTransfer = differentSignsaa | differentSignsbb
result(indexesToTransfer) = AA(indexesToTransfer)
Produces the output result you stated.

Matthew Whitaker
Matthew Whitaker 2013 年 2 月 18 日
Try:
AA = [ 0.1, 0.2, 0.3, 0.2, 0.1, -0.1, -0.2, -0.3];
aa = [0.2, 0.3, 0.2, 0.1, -0.1, -0.2, -0.3, NaN];
bb = [NaN, 0.1, 0.2, 0.3, 0.2, 0.1, -0.1, -0.2];
cc = AA;
e = sign(aa)-sign(bb);
cc(e==0|isnan(e)) = NaN
  1 件のコメント
Micky
Micky 2013 年 2 月 20 日
Thaanks Matthew

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by