フィルターのクリア

How can i access field values of a struct by indexing?

7 ビュー (過去 30 日間)
Henning Wilhelm
Henning Wilhelm 2017 年 11 月 6 日
コメント済み: Walter Roberson 2021 年 1 月 26 日
My struct has several fields containing a matrix. How can I get the first 100 values of each field without using a for loop.
a = b.(fieldnames(b))(1:100,:); % wont work
  1 件のコメント
Rik
Rik 2017 年 11 月 6 日
Playing around with struct2cell or struct2table could probably solve this.

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

採用された回答

Walter Roberson
Walter Roberson 2017 年 11 月 6 日
a = structfun(@(F) F(1:100,:), b, 'uniform', 0)
  4 件のコメント
Rik
Rik 2021 年 1 月 26 日
That is true in general. The best you can hope for is parity (given proper pre-allocation). Every function that hides a loop (cellfun, arrayfun, etc) will have an overhead. Matlab is getting better and better at for-loops.
The only exception to this rule is if you have actual array operations:
%slow:
s=0;for n=1:numel(data),s=s+data(n);end
%fast
s=sum(data);
Walter Roberson
Walter Roberson 2021 年 1 月 26 日
Anonymous functions are more expensive than plain function handles such as @sin

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by