Prev: what's the best way to find the number of consecutive blocks in an array?
Next: what's the best way to find the number of consecutive blocks in
From: Pr B on 26 Jan 2010 18:30 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.
From: Jan Simon on 26 Jan 2010 19:00 Dear Pr! > 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. It is hard to answer, if you do not ask a question. You noticed that your counter is buggy. Why don't you fix it? It would be helpful if you define "data1" and "data2" exactly (which one is which posted array?) and post your wanted output (with real numbers). I assume, that the bug in the counter is described by: " if the second column in both the arrays are equal to each other based on the unique value in column 1". You do not consider the values in column 1 of data1! General tip: Allocate the variable "percent" before the loop. Letting it grow repeatedly slows down Matlab dramatically. Better: percent = zeros(length(x), 2); ... percent(i, :) = [count/n, x(i)]; Good luck, Jan
From: Pr B on 26 Jan 2010 22:43 "Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <hjnvml$k9f$1(a)fred.mathworks.com>... > Dear Pr! > > > 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. > > It is hard to answer, if you do not ask a question. > You noticed that your counter is buggy. Why don't you fix it? > It would be helpful if you define "data1" and "data2" exactly (which one is which posted array?) and post your wanted output (with real numbers). > > I assume, that the bug in the counter is described by: " if the second column in both the arrays are equal to each other based on the unique value in column 1". You do not consider the values in column 1 of data1! > > General tip: Allocate the variable "percent" before the loop. Letting it grow repeatedly slows down Matlab dramatically. Better: > percent = zeros(length(x), 2); > ... > percent(i, :) = [count/n, x(i)]; > > Good luck, Jan the bottom array is data1 and the top array is data2. column 2 of data1 is a subset of column 2 of data2. here's the desired output: 165 1 168 some percentage .... i want to calculate percentage as: the overlapping values in column 2 of data1 and column 2 of data2 divided by the number of values in data1, both based on the unique value in column 1. how do i take into account the values in column 1 of the data when creating the counter?
From: Jos (10584) on 27 Jan 2010 05:05 "Pr B" <pb2297(a)columbia.edu> wrote in message <hjocoa$s4j$1(a)fred.mathworks.com>... > "Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <hjnvml$k9f$1(a)fred.mathworks.com>... > > Dear Pr! > > > > > 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. > > > > It is hard to answer, if you do not ask a question. > > You noticed that your counter is buggy. Why don't you fix it? > > It would be helpful if you define "data1" and "data2" exactly (which one is which posted array?) and post your wanted output (with real numbers). > > > > I assume, that the bug in the counter is described by: " if the second column in both the arrays are equal to each other based on the unique value in column 1". You do not consider the values in column 1 of data1! > > > > General tip: Allocate the variable "percent" before the loop. Letting it grow repeatedly slows down Matlab dramatically. Better: > > percent = zeros(length(x), 2); > > ... > > percent(i, :) = [count/n, x(i)]; > > > > Good luck, Jan > > the bottom array is data1 and the top array is data2. column 2 of data1 is a subset of column 2 of data2. > > here's the desired output: > > 165 1 > 168 some percentage > ... > > i want to calculate percentage as: > the overlapping values in column 2 of data1 and column 2 of data2 divided by the number of values in data1, both based on the unique value in column 1. > > how do i take into account the values in column 1 of the data when creating the counter? I do not understand your question. Can you give a simple and clear example of the inputs (A = [1 2 ; 1 3 ; 2 1 ; ...], B = [..]) and the expected output (C = [1 1 ; 2 0.4 ; ...]), and tell us why the output should hold exactly these numbers (C(1,:) = [1 1] because ...) ? Jos
From: Pr B on 27 Jan 2010 16:01
"Jos (10584) " <#10584(a)fileexchange.com> wrote in message <hjp350$9ro$1(a)fred.mathworks.com>... > "Pr B" <pb2297(a)columbia.edu> wrote in message <hjocoa$s4j$1(a)fred.mathworks.com>... > > "Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <hjnvml$k9f$1(a)fred.mathworks.com>... > > > Dear Pr! > > > > > > > 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. > > > > > > It is hard to answer, if you do not ask a question. > > > You noticed that your counter is buggy. Why don't you fix it? > > > It would be helpful if you define "data1" and "data2" exactly (which one is which posted array?) and post your wanted output (with real numbers). > > > > > > I assume, that the bug in the counter is described by: " if the second column in both the arrays are equal to each other based on the unique value in column 1". You do not consider the values in column 1 of data1! > > > > > > General tip: Allocate the variable "percent" before the loop. Letting it grow repeatedly slows down Matlab dramatically. Better: > > > percent = zeros(length(x), 2); > > > ... > > > percent(i, :) = [count/n, x(i)]; > > > > > > Good luck, Jan > > > > the bottom array is data1 and the top array is data2. column 2 of data1 is a subset of column 2 of data2. > > > > here's the desired output: > > > > 165 1 > > 168 some percentage > > ... > > > > i want to calculate percentage as: > > the overlapping values in column 2 of data1 and column 2 of data2 divided by the number of values in data1, both based on the unique value in column 1. > > > > how do i take into account the values in column 1 of the data when creating the counter? > > I do not understand your question. > > Can you give a simple and clear example of the inputs (A = [1 2 ; 1 3 ; 2 1 ; ...], B = [..]) and the expected output (C = [1 1 ; 2 0.4 ; ...]), and tell us why the output should hold exactly these numbers (C(1,:) = [1 1] because ...) ? > > Jos the problem is restated as follows: i have a double array of the following format: A = [1 14; 1 15; 1 16; 1 17; 1 18; 2 21; 2 24; 2 26; 3 39; 3 41; 3 56; 3 62; 4 17; 4 19; 4 28; 4 63; 4 81; 4 94] the first column of this array represents an "object" with the number 1, 2, 3, 4...the second column of this array can be thought of as "targets" of this object, meaning, the "object" 1,2,3, or 4 has an interaction with the value in the second column. i have another double array of the following format: B = [1 15; 1 16; 1 18; 2 21; 2 26; 3 39; 3 41; 3 56; 3 62; 4 17; 4 19; 4 63; 4 81; 4 94] the first and second columns of this array are exactly as i stated above. however, this array is missing rows that are present in array A. i would like to do the following: for each object: 1,2,3, and 4 i want to calculate the percentage of "targets" present in array B. as in, i want to count and identify the targets present in array B, and divide that number by the number of targets present in array A. for the example arrays above, i want the following array output: C = [1 .6; 2 .66; 3 1; 4 .83] where the first column represents the objects and the second column represents the percentage of surviving targets. to do this, i have written the following 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 i think this code has a bug in the counter. now that i have restated the problem, can anyone help? |