필터 지우기
필터 지우기

Executing same function in different elements of an array

조회 수: 3 (최근 30일)
Yanjika O
Yanjika O 2020년 5월 20일
댓글: Stephen23 2020년 5월 21일
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

채택된 답변

Stephen23
Stephen23 2020년 5월 20일
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
Yanjika O
Yanjika O 2020년 5월 21일
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 2020년 5월 21일
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.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by