MATLAB MATLAB if question for each elements

  • Thread starter Thread starter Hikarikaze
  • Start date Start date
  • Tags Tags
    Elements Matlab
AI Thread Summary
The discussion revolves around manipulating matrix elements in MATLAB to enforce a limit on values. Users seek to replace elements in a matrix that exceed a specified limit with that limit while retaining values below it. A specific example illustrates this by setting values above 5 in a vector to 5. Additionally, the conversation transitions to calculating the minimum total cost (TC) while also retrieving the corresponding value of NT at that minimum. The solution involves using MATLAB's min function to obtain both the minimum TC and its index, allowing for easy retrieval of the associated NT value.
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.
 

Similar threads

Replies
32
Views
4K
Replies
2
Views
2K
Replies
2
Views
2K
Replies
4
Views
2K
Replies
4
Views
2K
Replies
1
Views
4K
Replies
5
Views
1K
Back
Top