MATLAB if question for each elements

  • Context: MATLAB 
  • Thread starter Thread starter Hikarikaze
  • Start date Start date
  • Tags Tags
    Elements Matlab
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 3K views
Hikarikaze
Messages
4
Reaction score
0
Hi, I have a question about comparing each and every element in a matrix..
Heres what i have,
S1 = 90
S2 = 1
delta = 0.75
SM = 80
m = 0.1:0.01:100;

if (S1-S2*m.^(delta))>=SM
S_avg = SM

elseif (S1-S2*m.^(delta))<SM & (S1-S2*m.^(delta))>0
S_avg = (S1-S2*m.^(delta))

else S_avg = 0
end

I want all the values that exceed SM to be equal to SM (as it is the limit) or let the iteration just stop there and keep the number as it is if the value is below SM (below the limit).
for whatever input S1, S2, delta, and SM is..

for a different equation will be used for each case..
Is that possible?
 
Physics news on Phys.org
I haven't tried binary operations with entire matrices before but another approach that you can do is to subtract the working matrix from the constant matrix and if any element is negative then either set it/them to the desired value or just create a new matrix like SM*ones(length(m)).
 
Thanks for replying Viscous..
I don't really understand how you would approach this though..
Say i have (1 2 3 4 5 6 7 8 9 10)
I place a limit of 5.. So anything above 5 will be 5
Desired result: (1 2 3 4 5 5 5 5 5 5)
Is that possible??
 
Correct me if i understood you wrong but as an answer to your last question, i would do this:

Code:
%given a vector v
v = 1:10;
%predefined const
lim = 5;

for i = 1:length(v)
    if ( v(i) >5 ) %Checking the each element of the vector
        v(i) = 5; %Replaces the elements exceeding 5
    end
end

By the way, i did not understand why did you use element-wise operators?
 
Thanks ckutlu ! it works now..

I have another problem now though..
Please pardon my long message
but here's what I have..W = 1000;
L = 1000;
T = 10;
M = 25;
P1 = 300;
P2 = 10;
alpha = 1;
S1 = 115;
S2 = 3;
delta = 1;
SM = 90;
R1 = 0.5;
R2 = 0.007;
beta = 2;NT_min = W/M;

NT = NT_min:1:5*NT_min;
m = W./NT;

S = S1-S2.*m.^(delta);
for i = 1:length(S)
if (S(i) > SM)
S(i) = SM;
elseif (S(i) < 0)
S(i) = 0;
end
end

D = ceil(L./(T.*S));

TC = (2.*(P1+P2.*m.^(alpha)).*D + R1 + R2.*m.^(beta)).*(W./m);

The output i want is the minimum value of TC (which i can obtain by using min(TC))
But I also want the value of NT at minimum TC.. How can I do that?
 
Matlab help has just what you need =)

[C,I] = min(...) finds the indices of the minimum values of A, and returns them in output vector I. If there are several identical minimum values, the index of the first one found is returned.

so instead of doing output = min(TC), you get the indice by using this form of min function:

[output, indice] = min(TC);

then NT(indice) will give you the value of NT at minimum TC.