Using multiple structure fields in matlab without scrolling

I have an 8x18 structure with each cel containing the column vector of the occurrence of a single event. I want to get data from some of these fields combined into one array, without having to scroll through them. I can’t find a way to vertically concatenate the fields that interest me in a single array.

As an example, I create the following structure: from 1 to 5 entries in a cell:

s(62).vector(8,18).heading.occurrences=[1;2;3]; for i=1:62 for j=1:8 for k=1:18 y=ceil(rand(1)*5); s(i).vector(j,k).heading.occurrences=rand(y,1); end end end 

Now, if you want to get all occurrences in several cells, while maintaining the i constant for the instant i=1 , follow these steps:

 ss=s(1).vector([1 26 45]); h=[ss.heading]; cell2mat({h.occurrences}') 

Now I would like to do the same for s , for example s([1 2 3]).vector([1 26 45]) , how will this work? I tried xx=s([1 2 3]) , yy=xx.vector([1 26 45]) , but this, however, leads to an error:

Expected result from braces or indexing indexing points, but there were 3 results.

Is this also possible with vector operation?

+5
source share
2 answers

Here's a vectorized solution that takes into account index vectors for s and the vector field:

 sIndex = [1 2 3]; % Indices for s vIndex = [1 26 45]; % Indices for 'vector' field v = reshape(cat(3, s(sIndex).vector), 144, []); h = [v(vIndex, :).heading]; out = vertcat(h.occurrences); 

It uses cat to combine all the vector fields into a matrix of 8 by 18- numel(sIndex) , converts numel(sIndex) into a 144-byte numel(sIndex) , then indexes the rows specified by vIndex and collects their heading and occurrences using vertcat instead cell2mat .

+2
source

It is hard to digitize the whole operation, but it should work.

 % get vector field and store in cell array s_new = { s(1:3).vector }; % now extract heading field, this is a cell-of-cells s_new_heading = cellfun(@(x) { x.heading }', s_new, 'UniformOutput', false); occurences = {}; for iCell = 1:length(s_new_heading) % use current cell cellHere = s_new_heading{iCell}; % retain indices of interest, these could be different for each cell cellHere = cellHere([ 1 26 45 ]); % extract occurrences h = cellfun(@(x) x.occurrences, cellHere, 'UniformOutput', false); h_mat = cell2mat(h); % save them in cell array occurences = cat(1, occurences, h_mat); end 
0
source

Source: https://habr.com/ru/post/1269021/


All Articles