Maheen Siddiqui
Maheen Siddiqui

Reputation: 539

Repmat in Matlab to replicate specific elements

I have the following lines of code where I find an event "41" in my cell array called Events (minimal example of this array included below) and the lines of code allow me to add 8 events labeled "411" after "41" and this is output in out1 (a result of this is shown as well). My issue is that I need to include 5 entries of "411" BEFORE "41" and I'm not sure how to do it.

How can I modify the lines below to be able to do that easily?

out1 = arrayfun( @(x,b) [x; repmat({'411'}, 8*b, 1)], Events, strcmp('41',Events), 'uni', 0 );
out1 = vertcat(out1{:});
Events = ['9991'    '9991'  '9991'  '9991'  '9991'  '9991'  '9992'  '10'    '11'    '41'    '42'    '10'    '11'    '43'    '44'];

out1 = ['9991'  '9991'  '9991'  '9991'  '9991'  '9991'  '9992'  '10'    '11'    '41' '411' '411'  '411' '411' '411' '411' '411' '411'   '42'    '10'    '11'    '43'    '44'];

Thanks!

Upvotes: 1

Views: 96

Answers (1)

mhered
mhered

Reputation: 194

In order to help, could you be more specific to define the issue? Will the array “Events” always be single dimension? Do you expect that “41” will appear only once or multiple times?

I have no way to test it right now but I guess this should do it:

out1 = arrayfun( @(x,b) [repmat({'411'}, 5*b, 1), x], Events, strcmp('41',Events), 'uni', 0 ); out1 = vertcat(out1{:});

Let me know if you still have trouble with it and I can have a more in depth look this evening

Upvotes: 1

Related Questions