How to compare the values of 2 arrays vs each other?

Hi guys, I am a bit of a noob at Matlab. What I want to do is compare the values of two arrays with each other to see what % similar are they? So, basically, if I have array A = (1,2,3,4) and array B = (1,2,5,6)...I would like to compare these two to each other using a function or something, and that function should state (in this case) that they are 50% similar. I would appreciate your help.

 Risposta accettata

What do you mean by "similar"? If you want simply see the number of entries that are the same, then this could do the trick:
s = A==B; %this is a boolean vector that will be 1 if the entries are the same and 0 if different
similarity = sum(s)/numel(s); this is the number of entries that are equal divided by the total number of entries

5 Commenti

Hi njj1. As I used the example above, basically, if I have array A = (1,2,3,4) and array B = (1,2,5,6), I would like to compare these 2 arrays to each other and the how many numbers in each array match the other. In my example, 1 and 2 appears in both, therefore I would like to say that there is a 50% similarity between the arrays (half of the numbers in array B can be found in A). Thanks
Use the "intersect" function. If the vectors are the same length, then you can just divide the number of entries in the output by the number of entries in the arrays.
s = intersect(A,B);
similarity = numel(s)/numel(A);
The first method worked. Thank you very much. Can I ask you something else, how can I create an array that takes in values that are being fed from a variable that continuously changes values? Thanks
I'll try to answer your question as best I can understand it. I'm guessing that you have a for loop in which a variable that is being calculated, and you want to store those calculated variables into a vector for future analysis.
stored_variable = zeros(number_of_computations,1); %pre-allocate for speed
for i=1:number_of_computations
variable1 = i^2 + 2*i + 5; %compute your changing variable here
stored_variable(i) = variable1;
end
You can actually bypass the first computation and go directly to the stored variable like:
stored_variable = zeros(number_of_computations,1); %pre-allocate for speed
for i=1:number_of_computations
stored_variable(i) = i^2 + 2*i + 5; %compute your changing variable here and store it
end
If you have a more specific problem, I might be able to provide a better answer.
I worked around your answer and got to where I wanted to get. Thank you very much.

Accedi per commentare.

Più risposte (1)

You can simply use the following code to find at which index both arrays have same values.
Example:
same_index = find(array1 == array2);

1 Commento

This would find locations at which the values were the same, assuming that the two arrays were the same size.
However, the task is to determine " In my example, 1 and 2 appears in both" -- so the order and size of the two arrays can be different.

Accedi per commentare.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by