Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

sorting and summing through data

조회 수: 1 (최근 30일)
Tino
Tino 2019년 6월 3일
마감: MATLAB Answer Bot 2021년 8월 20일
hello please
I have the following data
for example T =
Drag Class
1 positive
3 positive
5 negative
7 positive
8 negative
6 positive
9 negative
2 positive
5 negative
9 negative
I want to write a code which will enable me to do the following computation
sort the data randomly
then when K = 1
k = 1 ( lowest positive / lowest negative) down the column
K = 2 ( lowest (positive ) + next lowest (positive) )/ (lowest ( negative) + next lowest ( negative) ) down the column till the end
K = 3 ( lowest ( positive) + next lowest ( positive) + next lowest ( positive) ) / ( lowest ( negative) + next lowest ( negative ) + next lowest ( negative)) down the column
This is done using if and conditional statement
Stop if no lowest number cannot be added to the sum anymore
Thanks in advance
Tino
  댓글 수: 2
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 6월 3일
Be specific? Consider simpler example for better illustrations.
Tino
Tino 2019년 6월 3일
Ok thanks for your swift response let me break it down. I have a table that have data and a label. 1 pos 2 neg 5 neg 6 pos 6 pos 7 neg
Down to n numbers How do I write the code For example The lowest 2 numbers for pos are 1 and 6 The lowest 2 numbers for neg 2 and 5
Then the computation = (1+6)/(2+5) =7/7 =1 Thanks in advance
Tino

답변 (1개)

Raghunandan V
Raghunandan V 2019년 6월 4일
편집: Raghunandan V 2019년 6월 4일
Hi,
I managed to solve the question. Please review and update
% positive is of class 1 and negetive is class 0 (just for ease of code)
A = [1 1; 3 1; 5 0; 7 1; 8 0; 6 1; 9 0; 2 1; 5 0; 9 0];
A_positive = A(A(:, 2) == 1);
A_negetive = A(A(:,2) == 0);
A_positive = sort(A_positive);
A_negetive = sort(A_negetive);
%get the length of positive and negetive numbers and len is taken as least of the both lengths
len = min(numel(A_positive), numel(A_negetive));
result = zeros(len,1);
for k = 1: len
result(k) = sum(A_positive(1:k))/sum(A_negetive(1:k));
end
result
if you want for only particular value of k. then remove the for loop and give specific value of k.
Regards,
Raghunandan V
  댓글 수: 2
Tino
Tino 2019년 6월 4일
Thanks for the answer Raghunandan V
Do you know how to convert column positive and negative to 1 and 0 without having to do it manually.
Thanks for your help in advance
Regards
Tino
Raghunandan V
Raghunandan V 2019년 6월 4일
What is the format of the data?
1. Is it excel?
If yes, you can use find and replace the data in excel itself.
2. was it created by matlab?
if yes, then check the code and replace the string 'positive' and 'negetive' by 0 and 1

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by