MATLAB if question for each elements

  • MATLAB
  • Thread starter Hikarikaze
  • Start date
  • #1

Main Question or Discussion Point

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?
 

Answers and Replies

  • #2
272
0
I havent 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 dont 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
32
0
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 heres 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
32
0
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 Threads on MATLAB if question for each elements

Replies
5
Views
23K
Replies
3
Views
649
Replies
0
Views
2K
Replies
2
Views
11K
Replies
3
Views
1K
Replies
8
Views
3K
Replies
16
Views
192K
Top