Efficient distribution of cell array in matlab

I have some that convert a cell array of strings to a cell array of characters.

Note. For a number of reasons, both input (C) and output (C_itemised) must be arrays of cells.

A cell array of strings (C) looks like this:

>> C(1:10)

ans = 

't1416933446'
''
't1416933446'
''
't1416933446'
''
't1416933446'
''
't1416933446'
''

      

I've only shown part of the array here. This is actually ~ 28,000 lines in length.

I have some code that does this, although it is very inefficient. The cellstr function takes 72% of the code time as it is currently called thousands of times. The code looks like this:

C_itemised=cell(length(C),500);
for i=3:length(C)
    temp=char(C{i});

    for j=1:length(temp)
        C(i-2,j)=cellstr(temp(j));
    end
end

      

I have a feeling that some minor modifications could take out the inner circuit, which will significantly reduce the overall runtime. I have tried several ways to do this, but I think I get confused all the time whether to use {} or () and couldn't find anything on the internet that can help me. Can anyone see a way to make the code more efficient?

Also note that this feature is used in conjunction with other features and works, although it is slower than ideal. So I don't want to change the format C_itemised

.

EDIT: (Sample) of the output of my current function:

C_itemised(1,1:12)

ans = 

Columns 1 through 12

't'    '1'    '4'    '1'    '6'    '9'    '3'    '3'    '4'    '4'    '6'    []

      

+3


source to share


1 answer


One thing I can suggest is to use an undocumented function sprintfc

. This function is hidden from normal use in MATLAB, but it is used internally with many other functions. Basically, if you tried to do it help sprintfc

, it will say no function found! It's nice to sniff the source sometimes!

The way it works sprintfc

is you provide it with a formatting string such as printf

, and the data you want to print. It will take every single piece of data and put it in separate cell arrays. As an example, let's say I had a string D = 'abcdefg';

if we did:

out = sprintfc('%c', D);

      

We get:

>> celldisp(out)

out{1} =

a


out{2} =

b


out{3} =

c


out{4} =

d


out{5} =

e


out{6} =

f


out{7} =

g

      

Thus, it takes each element in your string and puts them as separate characters serving as separate elements in a new cell array. The formatting line %c

means that we want to print one character for each element. Check out the link to the undocumented MATLAB I posted above if you'd like to learn more!




So try to simplify your loop:

C_itemised=cell(length(C));
for i=1:length(C)
    C_itemised{i} = sprintfc('%c', C{i});
end

      

C_itemised

will be a cell array where each element C_itemised{i}

is a different cell array, with each element in that cell array representing a single character that is made up of a string C{i}

.




Minor note

You said that you were confused in {}

and ()

in MATLAB for cells. {}

used to access individual elements within a cell. This way, C{1}

for example, will grab whatever is stored in the first element of the cell array. ()

used for slice and indexing in cells. For example, if you wanted to create another cell array that is a subset of the current one, you would do something like C(1:3)

. This will create a three-element array that has the first three cells in C

.

+3


source







All Articles