add internal parts of a vector
2 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Dyuman Joshi
2024-1-15
编辑:Dyuman Joshi
2024-1-15
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
%Convert the logical vector to a char vector
vec = sprintf('%d', idx)
%split the char vector with '0' as delimiter
vec = strsplit(vec, '0')
%Get the length of each sub-string
out = cellfun('length', vec)
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)
0 个评论
更多回答(3 个)
Stephen23
2024-1-15
A = [1;1;1;1;1;3;4;8;1;1;1;2;9;1;1;1;1;4;1;1]
X = A==1;
Y = cumsum([1;diff(X)>0]);
Z = accumarray(Y,X)
0 个评论
Hassaan
2024-1-15
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)
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.
0 个评论
Shivam
2024-1-15
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);
I hope it helps.
Thanks
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!