Info

This question is closed. Reopen it to edit or answer.

Different conditions, matching values

1 view (last 30 days)
Aragorn23
Aragorn23 on 9 Aug 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
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 Comment
KALYAN ACHARJYA
KALYAN ACHARJYA on 9 Aug 2019
Edited: KALYAN ACHARJYA on 9 Aug 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

Answers (1)

Neuropragmatist
Neuropragmatist on 9 Aug 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.

Community Treasure Hunt

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

Start Hunting!