Need help using a loop to find maximum
Show older comments
Here is my code so far:
%Finding the max N given z
clc; clear;
c = [2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3]; %example data set
N=length(c);
eta=4;
D=100;
for i = 1:N
q(i)=(D*(N-(1/eta)))^eta * ((1-(N-1)*eta)*c(i) + eta*(sum(c)-c(i)))/(sum(c)^(1+eta));
end
j=1;
for i = 1:N
if q(i) > 0
c_new(j)=c(i);
j=j+1;
end
end
N=length(c_new);
for i = 1:N
q_new(i)=(D*(N-(1/eta)))^eta * ((1-(N-1)*eta)*c_new(i) + eta*(sum(c_new)-c_new(i)))/(sum(c_new)^(1+eta));
end
So basically I am wanting to take a given data set, [c], run each variable in [c] through a function to produce a second dataset, [q]. If any value of q is negative, I want to get rid of those corresponding entries in [c], run [c] again through the function and get a new set of values in [q]. I want to keep doing this iteration and check until there are no negative values in [q]. In the end I should have some value for N, the number of entries in [c].
I am a bit inexperienced with programming and not sure how to capture this concept and put it in Matlab.
Your help is much appreciated.
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!