Executing same function in different elements of an array
2 views (last 30 days)
Show older comments
Hi,
I have wrote a function for small calculation. And I have an array 'A' which has over 25000 elements. I would like to refer certain ranges like A(2500:2800), A(5000:5300) and so on.., execute above mantioned function on them. Also, getting separate outputs from each range of A that I have selected. I have tried using array fun but I couldn`t make it work.
Please give your recommendations in this case.
Thank you
0 Comments
Accepted Answer
Stephen23
on 20 May 2020
Try something like this:
B = [2500,5000,...]; % begin indices
E = [2800,5300,...]; % end indices
N = numel(B);
C = cell(1,N);
for k = 1:N
V = A(B(k):E(k));
... do whatever with subvector V
C{k} = ... output of your code
end
2 Comments
Stephen23
on 21 May 2020
Replace the definitions of B and C with
B = [10,30,50,70,90];
C = [20,40,60,80,100]);
These are supposed to be vectors of indices. You used them to extract elements of A before the loop, and then in the loop you used those extracted elements of A as indices into A... which is unlikely to be very useful.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!