MATLAB vector intersection help

In summary, the code provided will give you a table with the unique values in column 1 and the percentage of overlap between the values in column 2 of array1 and array2. It loops over the unique values and checks for overlap between the values in column 2 for each unique value.
  • #1
hoffmann
70
0
i have the following double arrays:

165 7077
165 7045
165 5175
168 7305
168 3487
169 6196
169 10213
170 7048

165 7077
165 7045
168 5175
169 7305
169 3487
170 6196
170 10213
170 7048

how do i obtain the number and percentage of common values in column 2 of array1 and array2 by the unique values in column 1?

for which i wrote the code:

x = unique(data2(:,1));
n = length(data1);
percent = [];

for i = 1:length(x)
count = 0;
for j = 1:n
if data1(j) == x(i)
count = count+1;
end
end
percent=[percent;[count/n,x(i)]];
end

but this doesn't work. the counter is buggy in the if statement. i need to see if the second column in both the arrays are equal to each other based on the unique value in column 1. i need an output table something like:

165 %overlap

in two separate columns.
 
Physics news on Phys.org
  • #2
The following code should work:% Get unique values in column 1 of array1 and array2x = unique([data1(:,1); data2(:,1)]);% Pre-allocate space for the output percent = zeros(length(x), 2);% Loop over the unique valuesfor i = 1:length(x) % Get the indices where the unique value appears in both arrays idx1 = data1(:,1) == x(i); idx2 = data2(:,1) == x(i); % Get the values from column 2 for the given unique value col1_vals = data1(idx1,2); col2_vals = data2(idx2,2); % Calculate the overlap as a percentage overlap = length(intersect(col1_vals, col2_vals))/length(union(col1_vals, col2_vals)); % Store the results in the output array percent(i, :) = [x(i), overlap];end
 
  • #3


Hello,

Thank you for reaching out for assistance with your MATLAB code. It seems like you are trying to find the intersection of two vectors based on their unique values in the first column. Here are a few suggestions that may help you achieve your desired output:

1. First, make sure that your data is stored in a matrix format, with the first column representing the unique values and the second column representing the values you want to compare.

2. Use the "intersect" function in MATLAB to find the common values between the two vectors. This function will return a vector with the values that are present in both vectors.

3. To get the number and percentage of common values, you can use the "length" function to find the number of elements in the intersected vector, and then divide it by the length of one of the original vectors.

4. You can use the "fprintf" function to print out the results in a table format, with the unique values in the first column and the corresponding percentage of overlap in the second column.

Here is an example code that you can use as a guide:

% Create two vectors with the data provided
vector1 = [165 7077; 165 7045; 165 5175; 168 7305; 168 3487; 169 6196; 169 10213; 170 7048];
vector2 = [165 7077; 165 7045; 168 5175; 169 7305; 169 3487; 170 6196; 170 10213; 170 7048];

% Find the intersection of the two vectors based on the unique values in the first column
intersected_vector = intersect(vector1(:,1), vector2(:,1));

% Find the number and percentage of common values
num_common_values = length(intersected_vector);
percent_overlap = num_common_values / length(vector1) * 100;

% Print out the results in a table format
fprintf('Unique Value \t Percentage of Overlap\n');
fprintf('%d \t\t %.2f%%\n', [intersected_vector, percent_overlap]');

I hope this helps. If you continue to have issues with your code, don't hesitate to reach out for further assistance. Good luck with your project!
 

1. What is a vector intersection in MATLAB?

A vector intersection in MATLAB refers to the common elements between two or more vectors. It is the set of elements that appear in all of the given vectors.

2. How can I find the intersection of two vectors in MATLAB?

To find the intersection of two vectors in MATLAB, you can use the "intersect" function. This function takes in two or more vectors as inputs and returns a new vector with the elements that are common between all of the vectors.

3. Is there a way to find the intersection of more than two vectors in MATLAB?

Yes, the "intersect" function in MATLAB can take in multiple vectors as inputs. You can pass in as many vectors as you need to find the intersection of all of them.

4. Can I specify the order of the intersection elements in MATLAB?

Yes, you can specify the order of the intersection elements in MATLAB by using the "sorted" option in the "intersect" function. This will return the intersection elements in ascending order.

5. What if there are repeated elements in the input vectors for the intersection in MATLAB?

If there are repeated elements in the input vectors, the "intersect" function will only return one instance of each element in the intersection vector. If you want to include all repeated elements, you can use the "stable" option in the function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
8K
Back
Top