Effacer les filtres
Effacer les filtres

GENERATE CONDITIONS BETWEEN TWO ARRAYS AND RESULT IN A VALUE

2 vues (au cours des 30 derniers jours)
I have two matrices named AA and BB that are attached. I want to create conditions that relate the lines of the two and give me the result.
For example:
Condition 1
Values of AA>= 0 to AA<=5 and values of BB>=5 A to BB<=10, for all rows of both columns
Condition 2
AA values>= 5 to AA<=8 and BB values>=3 A to BB<=7 for all rows of both columns
Condition 3
AA values>= 3 to AA<=0 and BB values>=5 A to BB<=10 for all rows of both columnsThe result of these conditions will be in a matrix called AABB.
Being for condition 1 equal to 0.10, for condition 2 equal to 0.20 and for condition 3 equal to 0.30
-------
Below is explaining better how I want. I have my 3 conditions involving columns A and B, so I want a result in column AABB with the values 0.10, 0.20, 0.30. How do I solve the problem??
for AA >= 0 & AA <= 5 & BB >=5 & BB <= 10 % condition 1
AA >= 5 & AA <= 8 & BB >=3 & BB <= 7 % condition 2
AA >= 3 & AA <= 0 & BB >=5 & BB <= 10 % condition 3
AABB == 0.10 % results for the condition 1
AABB == 0.20 % results for the condition 2
AABB == 0.30 % results for the condition 3
end
Best Regard,
AP
  2 commentaires
KSSV
KSSV le 19 Nov 2022
You can use logical indexing and get the points. Did you try that?
Augusto Gabriel da Costa Pereira
No, how do I do it?

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 19 Nov 2022
Something like this?
load('AA.mat')
load('BB.mat')
disp([AA BB]) % check the data
4 -9 8 -7 -1 9 -9 -7 5 9 3 1 2 10 0 13 9 12 -5 13
AABB = zeros(size(AA));
AABB(AA >= 0 & AA <= 5 & BB >=5 & BB <= 10) = 0.1; % condition 1
AABB(AA >= 5 & AA <= 8 & BB >=3 & BB <= 7) = 0.2; % condition 2
AABB(AA >= 3 & AA <= 0 & BB >=5 & BB <= 10) = 0.3; % condition 3
disp(AABB); % check the result
0 0 0 0 0.1000 0 0.1000 0 0 0
Notice that condition 2 and 3 are never true and that condition 3 can never be true because it includes AA >= 3 & AA <= 0 and there are no numbers that would simultaneously satisfy those inequalities.
  1 commentaire
Augusto Gabriel da Costa Pereira
It works perfectly, it's simpler than I thought. Thank you very much.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 19 Nov 2022
condition1 = all(AA>=0 & AA<=5 & BB>=5 & BB<=10, 'all')
The result of these conditions will be in a matrix called AABB
You defined the conditions as only being true if they hold "for all rows of both columns" . Therefore condition1 and condition2 and condition3 are all scalars, so the result in AABB could only be a scalar, not a "matrix".
  4 commentaires
Walter Roberson
Walter Roberson le 20 Nov 2022
condition1 = all(AA>=0 & AA<=5 & BB>=5 & BB<=10, 2)
and appropriate condition2 and condition3, each using all() with 2 as the second parameter.
The result of each will be a row vector with the same number of rows as AA.
After that you can use logical indexing, such as
AABB = zeros(size(AA,1),1);
AABB(condition1) = 0.1;

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by