Sunday, 15 July 2012

Matlab: How to build array with strings at specific index -



Matlab: How to build array with strings at specific index -

i have cell array of strings (length = 4): a = {'a', 'b', 'c', 'd'} have double matrix of indices (length = 4): b = [2, 4, 6, 8]

how can create new cell array c (of strings) of length = 8, uses indices in b place strings a new array c. index not specified in b, want come in ' ' space (empty string). note: real info not go "every-other".

c = {' ', 'a', ' ', 'b', ' ', 'c', ' ', 'd'}

how can done in matlab?

one possible approach:

c = repmat({' '}, 1, max(b)); %// predefine ' '; c(b) = a; %// replace actual values

or:

c(b) = a; %// automatically fills missing values [] ind = cellfun('isempty', c); %// find occurrences of [] c(ind) = repmat({' '}, 1, sum(ind)); %// replace them ' '

the lastly line simplified follows (no repmat needed), noted @parags.chandakkar:

c(ind) = {' '}; %// replace them ' '

arrays matlab

No comments:

Post a Comment