How can I know if the repeated numbers of array A appear in B , and if so count their repetition (for A and B ) and divide their countings? A and B have different dimensions.

I have two files, and I want to count for both files how many times the values are repeated in a certain column. And if the repeated values appear in both files (for instance: value 0.5 appears repeated in file A and also in file B) I want their counting to be divided.
Example: in file A, column 1 I have 230 number "2" , 430 number "3"....etc
in file B, column 1 I have 500 number "2" , 620 number "3"....etc
After that I need to divide 230 by 430.
500 by 620....and so on.
That's what I did so far:
A = load('fileA.csv');
B = load('fileB.csv');
a = A(:,1);
b = B(:,1);
[count_a,mag_a]=hist(a,unique(a))
[count_b,mag_b]=hist(b,unique(b))
So, in mag_a I have a list of numbers, and in count_a how many times these numbers appear repeated. Same for mag_b and count_b.
I don't know how to tell the program something like: If number "x" is repeated in A and also in B, count and divide the counting.
Thank you!

 Akzeptierte Antwort

Hi Ana,
You may extract the common elements of the array using intersect() function.
common = intersect(mag_a, mag_b);
Then you may create an array of common counts:
countCommon_A = zeros(length(common),1);
countCommon_B = zeros(length(common),1);
for i=1:length(common)
idx = find(mag_a == common(i));
countCommon_A = count_a(idx);
countCommon_B = count_b(idx);
end
From here, you may divide the counts as needed.
Hope it helps!

3 Kommentare

Hi Athul,
Thanks for your help.
So, the countCommon_A and countCommon_B contain the countings for mag_a and mag_b considering the intersection, is that right?
For instance: the intersection here is from 0.2 to 4.1 (step of 0.1). So, in countCommon_A I have how many times 0.2 appears, 0.3, 0.4...until 4.1. Same for countCommon_B.
To divide their countings now (e.g., the counting of 0.2 in A by the counting of 0.2 in B, and so on..) I have to do countCommon_A ./ countCommon_B?
Yes that's right. I left out the division in my answer since I wasn't sure what you were trying to divide, but this sounds right.
I got it, it worked.Thank you so much, Athul!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Gefragt:

am 12 Mär. 2021

Kommentiert:

am 16 Mär. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by