Replacing the minimum of a vector

4 Ansichten (letzte 30 Tage)
Kalista Hall
Kalista Hall am 27 Aug. 2020
Kommentiert: Kalista Hall am 27 Aug. 2020
Hi
So I'm trying to replace the minimum value of a vector to infinity but when I test it, it's not usually the minimum value that gets replaced. It seems random, but that doesn't make sense.
x=input('Enter a vector for x');
x(min(x))=inf
I tried with [8,3,6,4] and it replaced the 6.
I tried with [7,3,5,2] and it replaced the 3.
I tried with [2,3,6,1] and it replaced the 2.
I have no idea where I'm going wrong. Anyone have any ideas? I'd really appreciate it.

Akzeptierte Antwort

Stephen23
Stephen23 am 27 Aug. 2020
Bearbeitet: Stephen23 am 27 Aug. 2020
" It seems random, but that doesn't make sense."
It is not random, and it makes perfect sense.
According to the min documentation "If A is a vector, then min(A) returns the minimum of A", so for your examples we would expect the following output values:
  • min([8,3,6,4]) -> 3
  • min([7,3,5,2]) -> 2
  • min([2,3,6,1]) -> 1
You then use those values as indices, which is a completely valid operation but those values are totally unrelated to the position of where the minimum values happen to be located in the original vector. So your examples are doing this:
  • index 3 -> replace 3rd element
  • index 2 -> replace 2nd element
  • index 1 -> replace 1st element
Does the minimum value of 3 tell us anything about its location? No, it does not. But you used it as an index to change the 3rd element's value to Inf: if you tell MATLAB to change the 3rd element's value, then that is what it does. It makes absolutely no difference where that 3 comes from.
If you want to replace the elements with that minimum value, then you will need the indices of those values, e.g.:
>> x = [8,3,6,4];
>> x(min(x)==x) = Inf
x =
8 Inf 6 4
where min(x)==x generates a logical index of all elements of x which are equal to min(x).
Another option is to obtain the second output of min, which also gives the index of the first minimum value:
>> x = [8,3,6,4];
>> [~,y] = min(x);
>> x(y) = Inf
x =
8 Inf 6 4
  1 Kommentar
Kalista Hall
Kalista Hall am 27 Aug. 2020
It makes sense now! Thank you for explaining where I was going wring. I've only been using Matlab for a month so I'm still working on figuring out all these little things. I appreciate your clarification.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Alan Stevens
Alan Stevens am 27 Aug. 2020
In your first one the minium value of x is 3, so Matlab replaced the third element with inf. Similar comments apply to the other examples. Try
x(x==min(x)) = inf;
instead.
  1 Kommentar
Kalista Hall
Kalista Hall am 27 Aug. 2020
I feel a little stupid now for not realising what was happening but thank you for clarifying for me. You just saved me hours of going crazy trying to figure it out!

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by