Info
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Different conditions, matching values
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi everyone,
I have a matrix with two columns of data. The data in the first column vary from 1 to 120. The data in the second column vary from 1 to 12.
A= [10 114 10 8 111 102 10 102 10].
B= [2 4 5 7 12 2 3 2 1].
I would like to calculate:
i) the number of times an element of column A (cA), between 100-109 is followed by a cA' value < 100 (indicating each of these elements);
ii) the number of times an element of cA > 109, is preceded by a cA' value < 100 (indicating each of these elements).
Additionally, that calculation should respect the elements of column B (cB) that match with the elements of cA >= 100.
For example, an output like this one.
cA cA cB n
114 10 4 1
111 8 1 1
102 10 2 2
How can I do that?
Thanks.
1 commentaire
KALYAN ACHARJYA
le 9 Août 2019
Modifié(e) : KALYAN ACHARJYA
le 9 Août 2019
Home Work??
Number of times an element of column A (cA), between 100-109
num_counts=sum(A(:,1)>100 & A(:,1)>109)
Do the similar approach for others, it may be home work, therefore try it and learn
Réponses (1)
Neuropragmatist
le 9 Août 2019
I don't really see how the output you give represents what you described in the question, or what you mean by
"that calculation should respect the elements of column B (cB) that match with the elements of cA >= 100"
However, i) and ii) can both be done with logical indexing:
A= [10 114 10 8 111 102 10 102 10];
B= [2 4 5 7 12 2 3 2 1];
% problem i)
idx1 = A>100 & A<109 & [A(2:end) NaN]<100
values = A(idx1)
num_times = sum(idx1)
% problem ii)
idx2 = A>109 & [NaN A(1:end-1)]<100
values = A(idx2)
num_times = sum(idx2)
Hope this helps,
M.
0 commentaires
Cette question est clôturée.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!