How to repeat value in Cell using MATLAB
Show older comments
Hello Everyone, I hope you are doing well
I have two cell array one is Value and other is Counts. Each Value in cell has specific count,
I want to repeat the element using count.
for example Value=1 its Counts=15 Then the values repeat 15 times , now the new cell has 15 Values for each [1,2,3,4] and it applied on ALL cells.
How can i do that in Matlab
Accepted Answer
More Answers (1)
Hello, I don't know if there is a builtin matlab function to do what you want, this should work:
Value = {1,2,3}
Count = {2,1,5}
%Not sure if you want to repeat the same value or count from the value up
%to count
Final = cellfun(@(x,y) repmat(x,1,y),Value,Count,'UniformOutput',0)
Final = cellfun(@(x,y) x+cumsum(ones(y,1))-1,Value,Count,'UniformOutput',0);
Final{1}, Final{2}, Final{3}
best,
Johan
4 Comments
Stephen john
on 27 Jun 2022
My bad I though it was single valued data, this should do what you want then:
Value = {[1; 2; 3; 4],[5; 6; 7; 8; 9]};
Counts = {[15; 15; 15; 15], [18; 15; 19; 15; 19]};
for i_cell = 1:length(Value)
Output{i_cell} = arrayfun(@(x,y) repmat(x,1,y), Value{i_cell}, Counts{i_cell}, 'UniformOutput', false);
end
Output{1}, Output{2}
cellfun(@length, Output{1}), cellfun(@length, Output{2})
Stephen john
on 28 Jun 2022
Stephen john
on 28 Jun 2022
Categories
Find more on Logical 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!