calculation of coordinates of the sum of values ​​greater than 5

조회 수: 2 (최근 30일)
Lev Mihailov
Lev Mihailov 2020년 2월 14일
댓글: Lev Mihailov 2020년 2월 14일
there is a vector of large length, here is a small part of it F1 = [2.5 2.5 3.5 1 1 5.2 11.4 4 4 5.2 2 2 6] I need to get the coordinate values ​​in this vector of all more than 5. I have a calculation code, but the F1 vector is too big and it takes a lot of time. Help me optimize the code.
ps. I specify all dimensions and create separately a region of zeros for all values
F1=[2.5 2.5 3.5 1 1 5.2 11.4 4 4 5.2 2 2 6]
Fzero=0;
Elemets=0;
j=0;
for i=1:length(F1)
Fzero=Fzero+F1(i);
Elemets=Elemets+1;
if Fzero>=5
j=j+1;
n(j)=Elemets;
psk(j)=Fzero;
Fzero=0;
Elemets=0;
end
end
% n =[2 3 1 1 2 1 3] % n i need to get

답변 (1개)

Rohan Kale
Rohan Kale 2020년 2월 14일
In my understanding you are trying to find the indices of values that are greater than 5. You can use the find function to actually optimize your code.
indices = find(F1 > 5);
That should solve your issue. I think the doc page on find will help you
  댓글 수: 1
Lev Mihailov
Lev Mihailov 2020년 2월 14일
This is the problem, I need exactly the sum of the elements greater than 5
indices = find(F1 > 5);
indices=[6 7 10 13]
%and I need to follow the example
F1=[2.5 2.5 3.5 1 1 5.2 11.4 4 4 5.2 2 2 6]
n(1)=2.5+2.5=5 ; % n(1)=2
n2(2)=3.5+1+1>5 ; % n(2)=3
...
I did not find a team in the matlab that could do this automatically

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by