Help with operations between elements from a matrix

1 view (last 30 days)
Hi, everybody
First of all, it is not a homework, it is a real operation that I need to do in my project, but I can't..
It is hard to explain, but I need to make a ponderation between elements with the same ID using the values from a column. The exactly operations are in the comments below:
26 1 % 26/26 = 1
35 2 % 35/35 = 1
27 3 % 27/27 = 1
25 4 % 25/25 = 1
41 5 % 41/(41+28+34) = 0.3981
28 5 % 28/(41+28+34) = 0.2718
34 5 % 35/(41+28+34) = 0.3398
34 6 % 34/(34+34) = 0.5
34 6 % 34/(34+34) = 0.5
29 7 % 29/(29+34) = 0.4603
34 7 % 34/(29+34) = 0.5397
24 8 % 28/28 = 1
35 9 % 35/(35+38+29+28) = 0.2692
38 9 % 38/(35+38+29+28) = 0.2923
29 9 % 29/(35+38+29+28) = 0.2231
28 9 % 28/(35+38+29+28) = 0.2154
40 10 % 40/(40+34) = 0.5405
34 10 % 34/(40+34) = 0.4594
The solution would be:
26 1 1
35 2 1
27 3 1
25 4 1
41 5 0.3981
28 5 0.2718
34 5 0.3398
34 6 0.5
34 6 0.5
29 7 0.4603
34 7 0.5397
24 8 1
35 9 0.2692
38 9 0.2923
29 9 0.2231
28 9 0.2154
40 10 0.5405
34 10 0.4594
I don't know if I made it really clear, but I really need help here.
Att,
Paulo

Accepted Answer

Voss
Voss on 28 Jan 2022
data = [ ...
26 1; ...
35 2; ...
27 3; ...
25 4; ...
41 5; ...
28 5; ...
34 5; ...
34 6; ...
34 6; ...
29 7; ...
34 7; ...
24 8; ...
35 9; ...
38 9; ...
29 9; ...
28 9; ...
40 10; ...
34 10; ...
];
data(:,end+1) = 0;
[uID,~,jj] = unique(data(:,2));
for ii = 1:numel(uID)
idx = jj == ii;
data(idx,end) = data(idx,1)./sum(data(idx,1));
end
disp(data);
26.0000 1.0000 1.0000 35.0000 2.0000 1.0000 27.0000 3.0000 1.0000 25.0000 4.0000 1.0000 41.0000 5.0000 0.3981 28.0000 5.0000 0.2718 34.0000 5.0000 0.3301 34.0000 6.0000 0.5000 34.0000 6.0000 0.5000 29.0000 7.0000 0.4603 34.0000 7.0000 0.5397 24.0000 8.0000 1.0000 35.0000 9.0000 0.2692 38.0000 9.0000 0.2923 29.0000 9.0000 0.2231 28.0000 9.0000 0.2154 40.0000 10.0000 0.5405 34.0000 10.0000 0.4595
  2 Comments
Paulo Cunha
Paulo Cunha on 29 Jan 2022
@Benjamin Thank you! You solved the problem with 6 lines! And very fast! This was awesome! Wow Thank you very much!!

Sign in to comment.

More Answers (0)

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!