add internal parts of a vector

2 views (last 30 days)
Me
Me on 15 Jan 2024
Answered: Stephen23 on 15 Jan 2024
Dear all, I have a problem with the sum of internal parts of a vector.I'll explain my problem below.I am analyzing a series of data, after having selected the vector (vectorA) with the quantities I need, I have to add the parts in which the terms equal to 1 follow each other. Example:
vectorA=[1;1;1;1;1;3;4;8;1;1;1;2;9;1;1;1;1;4;1;1]
I have to add only the 1 values (in groups), obtaining a vectorB which has as many terms as the groups of 1:
vectorB=[5;3;4;2].
In short, I need to know how many groups of terms equal to 1 I have and their sum. I tried with if loop, but I didn't succeed.
Thanks in advance for the help!

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 15 Jan 2024
Edited: Dyuman Joshi on 15 Jan 2024
Here is a simple way of dividing the data into groups of 1 and counting the number of elements for each sub-group -
%Converted to a row vector for displaying the values in a single line
vectorA=[1;1;1;1;1;3;4;8;1;1;1;2;9;1;1;1;1;4;1;1].';
%Values equal to 1
idx = vectorA == 1
idx = 1×20 logical array
1 1 1 1 1 0 0 0 1 1 1 0 0 1 1 1 1 0 1 1
%Convert the logical vector to a char vector
vec = sprintf('%d', idx)
vec = '11111000111001111011'
%split the char vector with '0' as delimiter
vec = strsplit(vec, '0')
vec = 1×4 cell array
{'11111'} {'111'} {'1111'} {'11'}
%Get the length of each sub-string
out = cellfun('length', vec)
out = 1×4
5 3 4 2
In case the data does not start or end with 1, there will be empty sub-strings present after splitting, as the first or the last character will be a delimiter; then remove the corresponding values -
out = out(out~=0)
out = 1×4
5 3 4 2

More Answers (3)

Stephen23
Stephen23 on 15 Jan 2024
A = [1;1;1;1;1;3;4;8;1;1;1;2;9;1;1;1;1;4;1;1]
A = 20×1
1 1 1 1 1 3 4 8 1 1
X = A==1;
Y = cumsum([1;diff(X)>0]);
Z = accumarray(Y,X)
Z = 4×1
5 3 4 2

Hassaan
Hassaan on 15 Jan 2024
vectorA = [1; 1; 1; 1; 1; 3; 4; 8; 1; 1; 1; 2; 9; 1; 1; 1; 1; 4; 1; 1];
vectorB = sum_consecutive_ones(vectorA);
disp(vectorB)
5 3 4 2
function vectorB = sum_consecutive_ones(vectorA)
% Ensure vectorA is a column vector
vectorA = vectorA(:);
% Initialize the count and the result vector
count = 0;
vectorB = [];
% Loop over the elements of vectorA
for i = 1:length(vectorA)
if vectorA(i) == 1
count = count + 1; % Increment count if the current element is 1
elseif count > 0
vectorB(end + 1, 1) = count; % Add the count to vectorB if the sequence ends
count = 0; % Reset the count
end
end
% If the last element of vectorA is 1, we need to add the count to vectorB
if count > 0
vectorB(end + 1, 1) = count;
end
end
---------------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Shivam
Shivam on 15 Jan 2024
Hi,
From the provided information, I understand that you want to identify and sum the consecutive ones in a vector and then record the length of each sequence of consecutive ones into a new vector.
You can follow the below workaround to achieve the goal:
% Initialize vectorA with specific values
vectorA = [1; 1; 1; 1; 1; 3; 4; 8; 1; 1; 1; 2; 9; 1; 1; 1; 1; 4; 1; 1];
n = length(vectorA);
% Initialize an empty array to hold the counts of consecutive ones
vectorB = [];
% Initialize a counter for consecutive ones
countOne = 0;
for i = 1:n
if vectorA(i) == 1
countOne = countOne + 1; % Increment the counter for consecutive ones
% If the current element is not 1 and there is a non-zero count of ones
elseif countOne
% Append the count of consecutive ones and
% reset the counterOne to 0
vectorB = [vectorB countOne];
countOne = 0;
end
end
% After the loop, if there is a non-zero count of ones left
if countOne
% Append the remaining count of consecutive ones to vectorB
vectorB = [vectorB countOne];
end
disp(vectorB);
5 3 4 2
I hope it helps.
Thanks

Categories

Find more on Dates and Time 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!