How to make this text string column program faster?

Accessions here are text strings with a length between 3 and 8. Here is my current program to process them:
% Accession:
ACCN = [];
c = length(Sta{i}.depth); % 1-column data
if isfield(Sta{i}, 'accession')
[ACCN{1:c}] = deal(Sta{i}.accession);
else
[ACCN{1:c}] = deal('N/A');
end
ACCESSION = [ACCESSION; ACCN'];
I'd want to convert the above program to a more effective way like the below program for processing numerical values:
Latitude = NaN(1000000, 1);
lastIndex = 0;
c = length(Sta{i}.depth); % 1-column data
if isfield(Sta{i}, 'lat')
Latitude([lastIndex+1:lastIndex+n],1) = ones(c,1) * Sta{i}.lat;
end
Latitude = Latitude([1:lastIndex],1);
How would I do this?
Thanks.

3 comentarios

dpb
dpb el 1 de Mzo. de 2020
Give us some sample data to work on and illustrate clearly what your inputs/desired outputs are...don't make us guess and then try to make up test data. "Help us help you..."
Leon
Leon el 2 de Mzo. de 2020
Here is an example data set.
Many thanks!
dpb
dpb el 2 de Mzo. de 2020
So what are we to make of that? What's the intended output? Every struct has the field so why the test?
Are you just trying to return the values of the field with the indices of the locations that don't have value for the field? Or is the field actually missing in the real dataset? If that's the case, looks like you should fix that instead and initialize the struct with a missing value instead and avoid the whole issue.

Iniciar sesión para comentar.

 Respuesta aceptada

dpb
dpb el 2 de Mzo. de 2020
Dunno how much better; still think to build the struct so all fields are extant would be far better; then you can use an array of struct as well instead of having to dereference the cell array. Remove as many levels of indirection as possible.
for i=1:numel(A)
d=numel(A{i}.depth);
try
ACCN=repmat({A{i}.accension},d,1);
catch
ACCN=repmat({'NA'},d,1);
end
end
If the size is large, the biggest overhead hit yet in both is the dynamic reallocation -- precompute the output size and store into the output array instead.

Más respuestas (0)

Productos

Versión

R2019b

Preguntada:

el 1 de Mzo. de 2020

Comentada:

el 5 de Mzo. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by