Info

此问题已关闭。 请重新打开它进行编辑或回答。

Different conditions, matching values

2 次查看(过去 30 天)
Aragorn23
Aragorn23 2019-8-9
关闭: MATLAB Answer Bot 2021-8-20
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 个评论
KALYAN ACHARJYA
KALYAN ACHARJYA 2019-8-9
编辑:KALYAN ACHARJYA 2019-8-9
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

回答(1 个)

Neuropragmatist
Neuropragmatist 2019-8-9
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!

Translated by