Executing same function in different elements of an array

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Stephen23
Stephen23 il 20 Mag 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 Commenti
Yanjika O
Yanjika O il 21 Mag 2020
Thank you for answering.
Here is what I tried so far. What I want here is
(1) Get the range of A from 10 to 20, 30 to 40, 50 to 60, 70 to 80 and 90 to 100
(2) Run the code below defining V vector starting from idx=V<5... for every range that I have defined on 1st step
(3) And get the summary result of how many of the element was met with my condition above by disp(f)
I still couldn`t fiure it out. There might be some wrong usafe of sth. Please suggest more.
A=(1:1:100);
B=A([10, 30, 50, 70, 90]);
C=A([20, 40, 60, 80, 100]);
N=numel(C);
D=cell(1:N);
for k=1:N
V=A(B(k):C(k));
idx=V<5;
ii1=strfind([0 idx 0],[0 1]);
ii2=strfind([0 idx 0],[1 0])-1;
ii=(ii2-ii1+1)>=5;
out=arrayfun(@(x,y)s(x:y),ii1(ii),ii2(ii),'un',0);
n=cellfun(@numel,cell(out));
for n=n(:,:)'
fd=(n/30);
f=sum(fd);
disp(f)
end
end
Stephen23
Stephen23 il 21 Mag 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by