MATLAB if question for each elements

In summary, to find the minimum value of TC and its corresponding NT value, you can use the min function in Matlab and get the indice of the minimum value to find the corresponding NT value.
  • #1
Hikarikaze
4
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
  • #2
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)).
 
  • #3
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??
 
  • #4
Correct me if i understood you wrong but as an answer to your last question, i would do this:

Code:
[COLOR="Green"]%given a vector v[/COLOR]
v = 1:10;
[COLOR="Green"]%predefined const[/COLOR]
lim = 5;

[COLOR="Blue"]for[/COLOR] i = 1:length(v)
   [COLOR="Blue"] if[/COLOR] ( v(i) >5 ) [COLOR="Green"]%Checking the each element of the vector[/COLOR]
        v(i) = 5; %Replaces the elements exceeding 5
   [COLOR="Blue"] end[/COLOR]
[COLOR="Blue"]end[/COLOR]

By the way, i did not understand why did you use element-wise operators?
 
  • #5
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?
 
  • #6
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.
 

Related to MATLAB if question for each elements

1. What is the purpose of using "if" statements in MATLAB?

"If" statements in MATLAB are used to control the flow of a program based on a certain condition. They allow the program to check if a certain condition is true or false, and then execute a specific set of code based on the result.

2. How do you write an "if" statement in MATLAB?

An "if" statement in MATLAB follows the format:
if (condition)
code to be executed if condition is true
end
The "if" keyword is followed by parentheses that contain the condition to be checked, and the code to be executed if the condition is true is written between the "if" statement and the "end" keyword.

3. Can an "if" statement have multiple conditions in MATLAB?

Yes, an "if" statement in MATLAB can have multiple conditions using logical operators such as "&&" (and) and "||" (or). For example:
if (condition1 && condition2)
code to be executed if both conditions are true
end
The code will only be executed if both conditions are true.

4. What is the purpose of using "else" statements in an "if" statement?

"Else" statements in an "if" statement are used to specify a set of code to be executed if the condition in the "if" statement is false. This allows for alternative code to be executed if the initial condition is not met.

5. Can an "if" statement be nested within another "if" statement in MATLAB?

Yes, "if" statements can be nested within each other in MATLAB. This means that an "if" statement can be written inside the code to be executed if the condition in another "if" statement is true. This allows for more complex conditions to be checked and different code to be executed based on those conditions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
4
Views
693
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top