Table manipulation of matrix
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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);
0 commentaires
Réponse acceptée
Guillaume
le 15 Mai 2019
Modifié(e) : Guillaume
le 15 Mai 2019
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
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!