Script for a sum of value drive by rules

조회 수: 1 (최근 30일)
Julien Boulay
Julien Boulay 2015년 8월 21일
답변: ag 2024년 9월 15일
Hi!
I have a matrix of data with only 0 and 0.2 values. I need to have the sum of successive 0.2 values (delta) for each columns of my matrix. Example: col1= 0 0 0 0.2 0.2 0.2 0.2 0 0 0.2 0 0.2 0.2 and the result_col1= 0.8 0.2 0.4. I write a script that works very well to do this (see above). First little problem, how can I say to my script that you need to read all columns of my matrix and put each correspondant delta values in my final matrix d?
Then, I would like to have a rule saying to the script "when you saw less than five zeros successively don't stop to calculate delta". In my script, the calculation of delta stop when the script saw only one zero after a 0.2 value.
Thanks a lot. Julien
T = importdata('data.xls')
T= T.Feuil1;
d(1,:) = zeros(1,size(T,2));
j=1;
for individu=1:size(T,2)
for i=1:size(T,1);
if T(i,individu)== 0.2 || any(T(1:i))==0
d(j,individu) = d(j,individu)+T(i,individu);
elseif T(i)==0 && T(i-1)== 0.2
j= j+1;
d(j,individu) = 0;
end
end
j=1;
end

답변 (1개)

ag
ag 2024년 9월 15일
Hi Julien,
I understand that you need help in modifying your code so that
  • The sum of delta gets stored, and resetted only after encountering five or more 0s
  • Calculate the deltas for all the columns, and store the result in a new matrix d
To do this, I kindly suggest you to follow the below steps:
  • For each column in your matrix, initialize variables to track the current delta sum and the count of successive zeros.
  • Traverse each element in the column.
  • If the element is 0.2, add it to the current delta sum and reset the zero counter.
  • If the element is 0, increment the zero counter.
  • If the zero counter reaches five, store the current delta sum in d and reset the delta sum and zero counter.
  • After processing all elements in a column, if there is a non-zero delta sum that hasn't been stored, append it to d. As the number of deltas calculated for each column might vary depending on the data, pad extra zeros to the calculated delta vector, before adding it to the matrix d.
  • Repeat the above logic for each column and store the results in the corresponding column of d.
The below dummy code explains the steps with a random matrix containing 0s and 0.2s as values:
% Initializing a 10x10 matrix with random values of 0 or 0.2
randomMatrix = 0.2 * (rand(10, 10) > 0.5);
% Initialize the matrix d to store results
d = [];
% Process each column
for col = 1:size(randomMatrix)
bufferCount = 0;
sumValue = 0;
sums = [];
for row = 1:size(randomMatrix, 1)
if randomMatrix(row, col) == 0.2
if bufferCount <= 5
sumValue = sumValue + 0.2;
else
disp("here")
sums = [sums; sumValue];
sumValue = 0.2; % Start new sum
end
bufferCount = 0; % Reset buffer count
elseif randomMatrix(row, col) == 0
bufferCount = bufferCount + 1;
end
end
% Store the last sum if it wasn't stored
if sumValue > 0
sums = [sums; sumValue];
end
% Append the sums of the current column to the matrix d
sums = [sums; zeros(10 - length(sums), 1)];
d = [d, sums];
end
% Display the result
disp('The calculated deltas for each column are stored in matrix d:');
disp(d);
Hope this helps!

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by