How to repeat value in Cell using MATLAB

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

 Risposta accettata

dpb
dpb il 27 Giu 2022
Modificato: dpb il 27 Giu 2022
fnRM=@(v,c)repmat(v,1,c);
for i=1:numel(Value)
C{i}=cell2mat(arrayfun(fnRM,Value{i},Counts{i},'UniformOutput',false).');
end
works if you replace the NaN in the Counts arrays with 0; you can't use a NaN in repmat.
You'll just have to define what is intended with the NaN Value elements -- are any NaN to show up in output or not.
ix=isfinite(Counts{i});
C{i}=cell2mat(arrayfun(fnRM,Value{i}(ix),Counts{i}(ix),'UniformOutput',false).');
inside the loop works to ignore the NaN entries as well without changing the data arrays.

4 Commenti

@dpb Getting Error using repmat
Replication factors must be a row vector of integers or integer scalars.
Error in @(x,y)repmat(x,1,y)
Have to see what you did in context -- works here w/ the downloaded .mat file when coded as above...
>> load matlab
>> whos Value Counts
Name Size Bytes Class Attributes
Counts 1x6 872 cell
Value 1x6 872 cell
>> fnRM=@(v,c)repmat(v,1,c);
>> for i=1:numel(Value),ix=isfinite(Counts{i});C{i}=cell2mat(arrayfun(fnRM,Value{i}(ix),Counts{i}(ix),'UniformOutput',false).');end
>> whos C
Name Size Bytes Class Attributes
C 1x6 2888 cell
>> C
C =
1×6 cell array
{1×60 double} {1×86 double} {1×98 double} {1×15 double} {1×12 double} {1×12 double}
>>
@dpb each cell is saving row wise , How can i save it in column wise
dpb
dpb il 28 Giu 2022
Modificato: dpb il 28 Giu 2022
Well, your original input cell arrays are row vectors...just transpose the output from cell2mat, of course...
>> for i=1:numel(Value),ix=isfinite(Counts{i});C{i}=cell2mat(arrayfun(fnRM,Value{i}(ix),Counts{i}(ix),'UniformOutput',false).').';end
>> C
C =
1×6 cell array
{60×1 double} {86×1 double} {98×1 double} {15×1 double} {12×1 double} {12×1 double}
>>
It's more bother to catenate vertically initially because they aren't all the same length.

Accedi per commentare.

Più risposte (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}
Value = 1×3 cell array
{[1]} {[2]} {[3]}
Count = {2,1,5}
Count = 1×3 cell array
{[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 = 1×3 cell array
{[1 1]} {[2]} {[3 3 3 3 3]}
Final = cellfun(@(x,y) x+cumsum(ones(y,1))-1,Value,Count,'UniformOutput',0);
Final{1}, Final{2}, Final{3}
ans = 2×1
1 2
ans = 2
ans = 5×1
3 4 5 6 7
best,
Johan

4 Commenti

@Johan Pelloux-Prayer not working on my dataset, i want to repeat value based on counts.
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}
ans = 4×1 cell array
{[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]} {[2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]} {[3 3 3 3 3 3 3 3 3 3 3 3 3 3 3]} {[4 4 4 4 4 4 4 4 4 4 4 4 4 4 4]}
ans = 5×1 cell array
{[ 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5]} {[ 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6]} {[7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7]} {[ 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8]} {[9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9]}
cellfun(@length, Output{1}), cellfun(@length, Output{2})
ans = 4×1
15 15 15 15
ans = 5×1
18 15 19 15 19
@Johan Pelloux-Prayer Getting Error using repmat
Replication factors must be a row vector of integers or integer scalars.
Error in @(x,y)repmat(x,1,y)
@Johan Pelloux-Prayer The Output Should be cell in which each cell has numeric array , Not cell present in cell.

Accedi per commentare.

Prodotti

Release

R2022a

Richiesto:

il 27 Giu 2022

Modificato:

dpb
il 28 Giu 2022

Community Treasure Hunt

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

Start Hunting!

Translated by