Hello, I have stored a lot of vectors in the fields of an structure array and i want to get every first element. s is an 1xN struct. field1 contains the vector
a = s(1:end).field1(1)
or
a = s.field1(1)
give the same error "Expected one output from a curly brace or dot indexing expression, but there were N results."

 採用された回答

Bruno Luong
Bruno Luong 2018 年 10 月 25 日
編集済み: Bruno Luong 2018 年 10 月 25 日

1 投票

If speed is matters, use for-loop
s = struct('field1',num2cell(rand(1,100000)));
tic
a = arrayfun(@(S) S.field1(1), s);
toc % Elapsed time is 0.598622 seconds.
tic
a = zeros(size(s));
for k=1:length(a)
a(k) = s(k).field1(1);
end
toc % Elapsed time is 0.051761 seconds.

2 件のコメント

Alexander Höll
Alexander Höll 2018 年 10 月 25 日
Thanks for the fast response :), seems like you can't always get around the for loop.
Walter Roberson
Walter Roberson 2018 年 10 月 25 日
You can use hidden for loops such as arrayfun or cellfun, but there is no built-in operation for this situation.

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

その他の回答 (1 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by