How to use histcounts with struct array?

조회 수: 3 (최근 30일)
Olaf van Buul
Olaf van Buul 2017년 12월 24일
댓글: Olaf van Buul 2017년 12월 26일
Hello,
I have a problem, I want to do this:
y = 10e-9*rand(270,1);
bins=linspace(0,10e-9,11);
[N,bincounts, binidx] = histcounts(y, bins);
So far no problem, however the value y is in my case the z-coordinate of a struct:
particle(i).coord(1:3);
I can get with a loop these values of particle(i).coord(3) in an array that I can use for input of histcounts.
But for performance reason I want direct access to the structure? Is this possible? I Googled and I found:
<https://nl.mathworks.com/help/matlab/matlab_prog/access-data-in-a-structure-array.html >
"You can index into part of a field only when you refer to a single element of a structure array. MATLAB® does not support statements such as S(1:2).X(1:50,1:80), which attempts to index into a field for multiple elements of the structure."
So I hope someone can help me.
Thanks for your time.
Regards,
Olaf
This is my first mail in this forum. However, I've read and learned a lot from it :)
  댓글 수: 1
Image Analyst
Image Analyst 2017년 12월 24일
I fixed your formatting. You can learn how to do it yourself here http://www.mathworks.com/matlabcentral/answers/13205#answer_18099
I assume you wanted to know how to vectorize and use handy built in functions like Jos showed you below, rather than for performance reasons. For a microscopic array with only 270 elements, no performance difference will be human noticeable at all between using a for loop and using arrayfun().

댓글을 달려면 로그인하십시오.

채택된 답변

Jos (10584)
Jos (10584) 2017년 12월 24일
Welcome to the forum!
You can use a loop or arrayfun to extract the scalar value, like:
y = arrayfun(@(k) particle(k).coord(3), 1:numel(particle)) ;
  댓글 수: 1
Olaf van Buul
Olaf van Buul 2017년 12월 26일
Hello Jos,
I coded
tic;
yyy = arrayfun(@(k) particle(k).coord(3), 1:numel(particle)) ;
toc;
tic;
for i=1:TOTAL_NUMBERS_ATOMS
y(i) = particle(i).coord(3);
end;
toc;
Elapsed time is 0.002761 seconds.
Elapsed time is 0.000274 seconds.
I was not expecting a difference of factor 10. As ImageAnalyst stated no performance difference will be human noticeable between them is true, except that I will be doing this at least 100000 times in my code.
Thanks both of you for your time.
Regards,
Olaf

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by