Structure initialization with repmat

前端 未结 2 576
天命终不由人
天命终不由人 2021-01-26 07:03

I want to initalize structures and it seems to be too slow. How do I do it with repmat, which is supposed to be a much faster solution in Matla

2条回答
  •  醉酒成梦
    2021-01-26 07:57

    Well in this case the first question is why are you using a cell array of structs. One of the fundamentals of matlab is that everything can be vectorised. So instead set up a matrix of structs.

    % Define the empty struct, struct itself is vectorised   
    myloc=struct('s',cell(10,30),'f',cell(10,30));
    %Assign values
    [myloc(:).s]=deal(0),[myloc(:).f]=deal(0);
    

    A matrix of structures is a far more powerful datatype. For example if you want to extract a single row of the field in to a vector you can simply use the following syntax

    sRow8=[myloc(8,:).s]
    

提交回复
热议问题