Hi
I want to a code that will compute group each c1/c2 for each column using K = to any number
Assuming I have n = numbers length
In my case I have 1,200 columns
And 1,200 rows
For instance, if K = 2
For each columns
The sum lowest c1 + c1 / the sum lowest c2 + c2
And it goes down the column for the next lowest c1 + c1/c2 + c2 till the end
If K = 3
Same the lowest c1 + c1 + c1/ the lowest c2 + c2 + c2
And it goes down the column for the next lowest c1 + c1 + c1/c2 + c2 + c2 till the end
I have been trying to work this out using the code below but but not getting it right. It is supposed to give me multiple answers for each column depending on K
c1 = sort(J(1:2:end,:));
size(c1)
c2 = sort(J(2:2:end,:));
size(c2
f = sum (c1(1: K, :));
size(f)
c = sum (c2(1: K,: ));
size(c)
s1 = (f/c);
for instance find the data below
A B C D E F G Class
1 1 1 0 1 1 1 c1
2 2 1 2 1 0 1 c2
1 1 1 1 2 1 1 c1
2 1 1 1 1 1 1 c2
2 2 0 0 0 0 0 c1
1 1 1 1 1 1 1 c2
1 1 1 1 1 1 1 c1
1 0 0 1 1 1 1 c2
Lets do column 1
First Sorting
1 c1
1 c1
1 c1
1 c2
1 c2
2 c1
2 c2
2 c2
K = 2
The lowest c1 + c1/ The lowest c2 + c2
For column A
1+1/1+1 = 2/2 = 1
The next lowest c1 + c1/The next lowest c2 + c2
1 + 2 / 2+2 = 3/2 = 1.5
So column 1 will be
1
1.5
Thanks for your help in advance
Tino
Find my code again
c1 = sort(J(1:2:end,:));
size(c1)
c2 = sort(J(2:2:end,:));
size(c2
f = sum (c1(1: K, :));
size(f)
c = sum (c2(1: K,: ));
size(c)
s1 = (f/c);

 Réponse acceptée

Guillaume
Guillaume le 15 Mai 2019
Modifié(e) : Guillaume le 15 Mai 2019

0 votes

Your question is really badly explained but I think I've understood what you want.
My understanding is that you have a J matrix where the odd rows are labeled 1 and even rows labeled 2. For each column, you want to sum the sorted odd rows over a non-overlapping sliding window of length K and divide that by the corresponding sum over the sorted even rows.
If so:
%J: a matrix whose number of rows is a multiple of 2*K.
%K: an integer, the window size
assert(mod(size(J, 1) / 2, K) == 0, 'Height of J is not a multiple of 2*K')
result = zeros(size(J, 1) / 2 / K, size(J, 2)); %preallocate result
for col = 1:size(J, 2) %loop over the columns. You can't do this without a loop due to the requirement to sort each columns separately
result(:, col) = sum(reshape(sort(J(1:2:end, col)), K, []), 1) ./ sum(reshape(sort(J(2:2:end, col)), K, []), 1); %sort even/odd rows, reshape each into a Kx? matrix, sum across rows and divide the two sums
end

10 commentaires

Tino
Tino le 15 Mai 2019
Hi Guillaume
Thanks for your response.
I run the code and am getting
Undefined function or variable 'L'.
What does L means
Thanks in advance
Tino
Guillaume
Guillaume le 15 Mai 2019
Basic typo, that L should have been K. Letters are next to each other on the keyboard.
Fixed now.
Tino
Tino le 15 Mai 2019
Hi Guillaume
I am experimenting the code you gave to me on a sample matrix
and I am getting the error below
Error using fishy (line 17)
Height of J is not a multiple of 2*K
why?
find the sample dataset below
T =[
5.1000 3.5000 1.4000 0.2000;
6.4000 3.2000 4.5000 1.5000;
5.4000 3.4000 1.7000 0.2000;
5.7000 2.8000 4.5000 1.3000;
5.7000 4.4000 1.5000 0.4000;
5.6000 2.9000 3.6000 1.3000];
z = T(:,:);
K = 2;
assert(mod(size(z, 1) / 2, K) == 0, 'Height of J is not a multiple of 2*K')
result = zeros(size(z, 1) / 2 / K, size(z, 2)); %preallocate result
for col = 1:size(z, 2)
%loop over the columns. You can't do this without a loop due to the requirement to sort each columns separately
result(:, col) = sum(reshape(sort(z(1:2:end, col)), K, []), 1) ./ sum(reshape(sort(z(2:2:end, col)), K, []), 1); %sort even/odd rows, reshape each into a Kx? matrix, sum across rows and divide the two sums
end
size(result)
Error using fishy (line 17)
Height of J is not a multiple of 2*K
Guillaume
Guillaume le 15 Mai 2019
As I wrote in a comment on the first line of the code, the number of rows of J must be a multiple of 2*K. My code then check that it is the case and if not, throws the error you see.
Since you're dividing the odd (and even) rows in groups of length K, the number of odd (and even) rows must be a multiple of K, otherwise one of the window can't be of length K. And since you need the same number of odd and even rows, the height of J must be a multiple of 2*K.
If you don't want the code to error if that precondition is broken, you need to explain what should be done then.
Tino
Tino le 15 Mai 2019
Hi Guillaume
Thanks for your response
But a given data might not really satisfy the condition 2K. in that case the calculation must stop and the answers provided from before the condition is not met instead of the error.
And how do I do that
Regards
Tino
So basically, you want to crop J to a multiple of 2*K:
%J: a matrix of any size
%K: window size
croppedJ = J(1:end-mod(end, 2*K), :); %ensure that height of J is a multiple of 2*K by removing extra elements from the end
result = zeros(size(croppedJ, 1) / 2 / K, size(croppedJ, 2)); %preallocate result
for col = 1:size(croppedJ, 2) %loop over the columns. You can't do this without a loop due to the requirement to sort each columns separately
result(:, col) = sum(reshape(sort(croppedJ(1:2:end, col)), K, []), 1) ./ sum(reshape(sort(croppedJ(2:2:end, col)), K, []), 1); %sort even/odd rows, reshape each into a Kx? matrix, sum across rows and divide the two sums
end
Tino
Tino le 16 Mai 2019
You are brilliant Guillaume and Thanks a lot
Assuming I have 3 classes and wishes to use this code above. The classes ( for instance c1, c2, c3) can assigned to any regardless of if it is even or odd.
Can I just replace where u have 2 with 3 or will it work that way
Thanks in advance
Guillaume
Guillaume le 16 Mai 2019
If you had more than two classes, I would have used a completely different method to identify the elements of each. It still would be very simple but nothing like the code above.
However, I don't see how you can extend the code to more than two classes, since the main step is dividing the elements of one class by the elements of the others. If you have more than 2 classes, what are you dividing by what?
Tino
Tino le 16 Mai 2019
Thanks for the answer. What if I have 3 classes what is the completely different method you will use to identify it.
Thanks for your persistence help
I really appreciate
Regards
Tino
Guillaume
Guillaume le 16 Mai 2019
You'd use findgroups or the older unique to identify the groups, then splitapply or the older accumarray to apply the processing to each group.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Centre d'aide et File Exchange

Produits

Version

R2019a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by