フィルターのクリア

Calculations for arrays inside an array

2 ビュー (過去 30 日間)
Jasmine Karim
Jasmine Karim 2018 年 8 月 29 日
コメント済み: Andrei Bobrov 2018 年 8 月 29 日
I have a 1x1 structure with 6 fields. In each field is a cell array comprised of various lengths x 8 columns. I would like to write a loop that will perform a specific calculation for EACH cell array i.e.) subtract column 5 from 4, then compute the average.
I can't seem to get the loop going, I keep getting errors such as "Cell contents reference from a non-cell array object. ".
Any suggestions are greatly appreciated.
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2018 年 8 月 29 日
Where is your example? Please attach an example of your structure, as a mat-file.

サインインしてコメントする。

回答 (1 件)

dpb
dpb 2018 年 8 月 29 日
編集済み: dpb 2018 年 8 月 29 日
Making the array content cell arrays complicates dereferencing but something like
structfun(@(c) mean(c{:}(:,5)-c{:}(:,4)),s)
works for specific column indices.
The above assumes the structure is like
>> s.f1={rand(3,4)};
>> s.f2={rand(5,4)}
s =
struct with fields:
f1: {[3×4 double]}
f2: {[3×4 double]}
>>
There's no reason that the struct needs the arrays as cell arrays, though, each field content is independent so you can have differing-sized double arrays and use direct addressing --
>> s.f1=rand(3,4);
>> s.f2=rand(5,4)
s =
struct with fields:
f1: [3×4 double]
f2: [5×4 double]
>>
Then, it's just
structfun(@(c) mean(c(:,5)-c(:,4)),s)
as structfun pass each field in turn which is now just a double array instead of cell containing a double array.
Of course, if your structure is some other perturbation, we'll need to know what that is to know how to dereference it properly.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by