2017-06-20 2 views
5

私は、各セルが1つのイベントの発生の列ベクトルを含む8x18構造体を持っています。私は、これらのフィールドのいくつかから単一の配列に連結されたデータをループしないで取得したいと考えています。興味のあるフィールドを1つの配列に垂直に連結する方法を見つけることができないようです。私は、セルあたり1〜5の出現と次のような構造を作成する例としてループしないでmatlab内の複数の構造体フィールドにアクセスする

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 

今インスタントi=1次作品のためにiを一定に保ちながら、いくつかの細胞ではすべてのオカレンスを取得する場合:

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

今、私はどのように働くだろう、例えばs([1 2 3]).vector([1 26 45])ため、sのために同じことをやりたいのでしょうか?私はxx=s([1 2 3])yy=xx.vector([1 26 45])を試してみましたが、これはしかし、エラーが得られます

Expected one output from a curly brace or dot indexing expression, but there were 3 results.

が、これはベクトル演算でも可能ですか?

答えて

2

sとフィールドvectorのインデックスベクトルを用い収容ベクトル化溶液は次のとおり

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); 

それは8×18バイnumel(sIndex)に全てvectorフィールドを連結するcatを使用しますマトリックスを144行目のnumel(sIndex)のマトリックスに入れ、次にvIndexで指定された行を索引付けし、の代わりにvertcatを使用して、それらのheadingおよびoccurrencesフィールドを収集します。

+0

賢い、ありがとう! – Jasper

0

操作全体をベクトル化することは難しいですが、これはうまくいくはずです。ここ

% 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 
関連する問題