Min and max value of many results

6 visualizaciones (últimos 30 días)
N/A
N/A el 13 de En. de 2021
Comentada: Jan el 14 de En. de 2021
I have a code like below
w=0:0.001:1.393;
for i=1:length(w)
k=2;
tao=2;
T=tan((tao*w(i)-k*pi)/2)/w(i)
end
When I run this code, results like this are coming (this is part of it)
T =
3.8973
T =
3.9164
T =
3.9357
T =
3.9553
T =
3.975
T =
3.995
How can I write a min and max value of T in one vector?
Like this for above example:
[3.8973, 3.995]
[min value, max value]
Can you help me? Thank you

Respuestas (2)

Jan
Jan el 13 de En. de 2021
Editada: Jan el 14 de En. de 2021
Use a vectorized method to obtain the results as a vector, not as a bunch of single variables:
w = 0:0.001:1.393;
k = 2;
tao = 2;
T = tan((tao * w - k * pi) / 2) ./ w;
Then min and max can be applied directly:
Result = [min(T), max(T)];
Alternatively with a less efficient loop:
w = 0:0.001:1.393;
k = 2; % Define constants before the loop
tao = 2;
T = zeros(1, numel(w)); % Pre-allocate
for iw = 1:numel(w) % Prefer NUMEL instead of LENGTH
T(iw) = tan((tao * w(iw) - k * pi) / 2) / w(iw);
end
% [EDITED] "for k" -> "for iw"
Then the above command to obtain the result is working also.
  3 comentarios
N/A
N/A el 13 de En. de 2021
I should take 1.0354 for min value.
Jan
Jan el 14 de En. de 2021
The -Inf was a mistake in my loop code: I've used "k" as loop counter, but it is a constant also. This is fixed now.
The vectorized code replies the same value as the loop now. For the 2nd element of w, the value of T is 1.00000033333348, which is smaller than the minimal value you assume. Why do you think it is 1.0354?
w = 0:0.001:1.393;
iw = 2;
k = 2;
tao = 2;
T = tan((tao * w(iw) - k * pi) / 2) / w(wi)

Iniciar sesión para comentar.


Daniel Pollard
Daniel Pollard el 13 de En. de 2021
Good news! Matlab has built in min and max commands. So it's pretty straightforward to define them and store them in a vector:
Tmin = min(T);
Tmax = max(T);
Tminmax = [Tmin, Tmax];
Just like that.
  4 comentarios
Adam Danz
Adam Danz el 13 de En. de 2021
@Dorukhan Astekin , this is the same exact conversation we had earlier today that you deleted. You can't delete this one because it has answers but the lesson to be learned is that deleting a conversation and reposting the same question isn't going to help you.
If you want free help from volunteers, you need to work with them and address their questions.
Daniel is recommending the same thing I recommended to you an hour or more ago. You've wasted your own time as well as others rather than trying to understand the suggestions and clarify the problem.
Adam Danz
Adam Danz el 13 de En. de 2021
Editada: Adam Danz el 13 de En. de 2021
> "Thank you for your answer. But it is valid for matrices."
This was the same response you gave me hours prior.
In the example in your question,
T =
3.8973
T is a matrix.
T = 3.8937;
ismatrix(T)
ans = logical
1
>you didn't understand my question
You're right that I didn't understand your question because you haven't explained the problem well and your examples do not agree with your descriptions. The addition of a for-loop in this version of your question was helpful to show the problem.
> They gave better answers than you.
I love when I see better answers. But both answers here gave the same answer I did: to use min() and max(), which didn't work for your data for reasons that were not clear until you added the for-loop explaining how you're creating T.
> Please, don't crowd under my question.
An important lesson is here to work with volunteers who are give you their time. Sometimes it's difficult to describe a problem and that often requires some back-and-forth comments so others can understand the data you're working with, your goal, and the problem. Part of helping others solve these problems is helping them to communicate them in the first place with is why I'm trying to drill this message in. When you delete a thread that contains commentary and ask a new question, it's a message that you do not value the time of other contributors whose main goal is to help you.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by