Using a logic mask for selective data operations

Hi All. I'm trying to use a logic mask to select data in a numerical matrix, rather than simply looping through each cell, and perform some mathematical operation, and then the inverse maks to perform a different operation. For example...
data = [-2, -4, 1, 3, 4, -6; -0.5, 2.4, 5, 9.8, 3, -11.2; 4, 3, -2, -1.5, 7, 10; 3, 4, -2.4, -3.3, -8, 10];
logicmaskneg = data < 0;
logicmaskpos = data > 0;
dataneg = data(logicmaskneg);
datapos = data(logicmaskpos);
So logicmaskneg and logicmaskpos keep the same matrix structure as data. But dataneg and datapos collapses the data matrix into one column. How do I apply the logic masks such that I can keep dataneg and datapos in the matrix organisation as data so I can continue to perform further calculations preserving the original column and row organisation?
Maybe I'm going about this the wrong way. My aim is to perform one operation on only the negative numbers and a different calculation on the positive numbers while preserving the data layout.
Any help and advice is much appreciated.
Thanks.

 採用された回答

Star Strider
Star Strider 2019 年 12 月 18 日

0 投票

The ‘logicmaskneg’ and ‘logicmaskpos’ matrices become numeric matrices when used in any calculation.
See if this does whatt you want:
data = [-2, -4, 1, 3, 4, -6; -0.5, 2.4, 5, 9.8, 3, -11.2; 4, 3, -2, -1.5, 7, 10; 3, 4, -2.4, -3.3, -8, 10];
logicmaskneg = data < 0;
logicmaskpos = data > 0;
dataneg = data(logicmaskneg);
datapos = data(logicmaskpos);
producing:
dataneg =
-2 -4 0 0 0 -6
-0.5 0 0 0 0 -11.2
0 0 -2 -1.5 0 0
0 0 -2.4 -3.3 -8 0
datapos =
0 0 1 3 4 0
0 2.4 5 9.8 3 0
4 3 0 0 7 10
3 4 0 0 0 10

8 件のコメント

Star Strider
Star Strider 2019 年 12 月 18 日
Wah On Ho’s Answer moved here —
Hi Star Strider, Yes your output result is what I'm after. How did you do this as the code is the same as my code and only produces a single column of results. Once I've made the calculations on dataneg and datapos, I want to combine them into one matrix with both sets of results, i.e. the zeros of one matrix is replaced (transposed) with the results of the other matrix. Thanks.
Star Strider
Star Strider 2019 年 12 月 18 日
‘... as the code is the same as my code ...’
I copied your code to my Answer by mistake. My apologies.
I intended to post:
data = [-2, -4, 1, 3, 4, -6; -0.5, 2.4, 5, 9.8, 3, -11.2; 4, 3, -2, -1.5, 7, 10; 3, 4, -2.4, -3.3, -8, 10];
logicmaskneg = data < 0;
logicmaskpos = data > 0;
dataneg = data.*logicmaskneg
datapos = data.*logicmaskpos
So they are not the same. The difference is that I multiplied the logical matrices by the original matrix to get the two different results. This code will produce the result I posted.
Wah On Ho
Wah On Ho 2019 年 12 月 18 日
Ahh...I completely overlooked about multiplying the data matrix to the logicmask matrix, element wise. Thank you.
Do you know how I would merge dataneg and datapos into one matrix comprising the results of both matrices?
Star Strider
Star Strider 2019 年 12 月 18 日
My pleasure.
It depends on what you mean by ‘merge’.
Adding them (once you have done the necessary calculations on each one) is straightforward.
To multiply them element-wise (once you have done the necessary calculations on each one), it would first be necessary to convert all the 0 values to 1. That is easily done using the logical mask matrices already created.
Wah On Ho
Wah On Ho 2019 年 12 月 18 日
Sorry I wasn't being clear. To continue, say 'dataneg' is multiplied by 2, and 'datapos' is divided by 2. Now I want to combine both matrices together, call it 'databoth', such that all the zero values of 'dataneg' are replaced with the values from 'datapos'. I'll then use 'databoth' for further mathematical operations. You can probably tell I'm still learning Matlab but what better way to learn it while I use it, and pick expert brains when I get stuck. Thanks again for your help.
Star Strider
Star Strider 2019 年 12 月 18 日
No worries. However I do need to know what result you want.
Try this:
data = [-2, -4, 1, 3, 4, -6; -0.5, 2.4, 5, 9.8, 3, -11.2; 4, 3, -2, -1.5, 7, 10; 3, 4, -2.4, -3.3, -8, 10];
logicmaskneg = data < 0;
logicmaskpos = data > 0;
dataneg = data.*logicmaskneg
datapos = data.*logicmaskpos
databoth = datapos/2 + dataneg*2
producing:
databoth =
-4 -8 0.5 1.5 2 -12
-1 1.2 2.5 4.9 1.5 -22.4
2 1.5 -4 -3 3.5 5
1.5 2 -4.8 -6.6 -16 5
MATLAB makes these sorts of calculations straightforward.
Wah On Ho
Wah On Ho 2019 年 12 月 18 日
That's exactly what I'm after! I agree, Matlab does make these calculations straightforward once you know. Thank you so much.
Star Strider
Star Strider 2019 年 12 月 18 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

製品

リリース

R2019a

タグ

質問済み:

2019 年 12 月 18 日

コメント済み:

2019 年 12 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by