MATLAB if question for each elements

  • Context: MATLAB 
  • Thread starter Thread starter Hikarikaze
  • Start date Start date
  • Tags Tags
    Elements Matlab
Click For Summary

Discussion Overview

The discussion revolves around manipulating matrices in MATLAB, specifically focusing on element-wise comparisons and conditional assignments. Participants explore how to set limits on matrix values and retrieve corresponding indices for minimum values in calculations.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant seeks to compare each element in a matrix against a threshold and replace values exceeding that threshold with a specified limit.
  • Another participant suggests using binary operations to create a new matrix where values exceeding the limit are set to that limit.
  • A participant provides an example of how to implement a loop to replace elements in a vector based on a limit.
  • Further, a participant describes a more complex scenario involving multiple parameters and calculations, asking how to find the minimum value of a derived matrix and the corresponding index from another variable.
  • Another participant responds with a MATLAB function that retrieves both the minimum value and its index, suggesting a method to access the corresponding variable value.

Areas of Agreement / Disagreement

Participants generally agree on the methods to manipulate matrix values and retrieve indices, but there are varying approaches and levels of understanding regarding the implementation details.

Contextual Notes

Some participants express uncertainty about the use of element-wise operators and the specifics of matrix manipulation, indicating a need for clarification on these topics.

Who May Find This Useful

Individuals interested in MATLAB programming, particularly those working with matrix operations and conditional logic in computational tasks.

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 ·
2
Replies
32
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K