Print the first seven values from the sorted vector with value and index to the new vector.

1 view (last 30 days)
Hi, I would like to write out the first seven values from the sorted vector "Tablica' with value and index to the new vector and then find the smalest values and index (but i have to be the same index like in sorted vector 'Tablica'). The smallest vales has to have "old index". On the other hand if values are the same i have to make other calculations like in my code but if the values are diffrent write out the smallest values and 'old index'
Thank you in advance.
  5 Comments

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 2 Dec 2021
Does this do what you want?
wekotr_1 = randi(99)
wekotr_2 = randi(99)
wekotr_3 = randi(99)
wekotr_4 = randi(99)
wekotr_5 = randi(99)
wekotr_6 = randi(99)
wekotr_7 = randi(99)
wekotr_8 = randi(99)
wekotr_9 = randi(99)
wekotr_10 = randi(99)
wekotr_11 = randi(99)
Tablica = [wekotr_1, wekotr_2, wekotr_3, wekotr_4, wekotr_5, wekotr_6, wekotr_7, wekotr_8, wekotr_9, wekotr_10, wekotr_11];
% Sort vector..
[odl, sortOrder] = sort(Tablica);
najblizsze_kNN = odl(1:7)
Indeksy_kNN = sortOrder(1:7)
if all(odl(1:7) == odl(1))
% All of the first 7 values are the same.
w_1= 1/(1+(odl(1))^2)
w_2= 1/(1+(odl(2))^2)
w_3= 1/(1+(odl(3))^2)
w_4= 1/(1+(odl(4))^2)
w_5= 1/(1+(odl(5))^2)
w_6= 1/(1+(odl(6))^2)
w_7= 1/(1+(odl(7))^2)
W=[w_1, w_2, w_3, w_4, w_5, w_6, w_7]
[wartosc index_w]=min(W)
else
% Find index of smallest value in original, unsorted vector.
[minValue, indexOfMinValue] = min(Tablica)
end
  2 Comments
Pawel Szczepanowski
Pawel Szczepanowski on 2 Dec 2021
Not that all becouse the final minValue has to be from the first seven values of sorted vector and index has to be from sorted vecotr(Tablica)
Image Analyst
Image Analyst on 2 Dec 2021
Note I did
if all(odl(1:7) == odl(1))
and odl is the sorted vector. To use the index from the sorted vector instead of the original vector just use odl in the if block:
wekotr_1 = randi(99)
wekotr_2 = randi(99)
wekotr_3 = randi(99)
wekotr_4 = randi(99)
wekotr_5 = randi(99)
wekotr_6 = randi(99)
wekotr_7 = randi(99)
wekotr_8 = randi(99)
wekotr_9 = randi(99)
wekotr_10 = randi(99)
wekotr_11 = randi(99)
Tablica = [wekotr_1, wekotr_2, wekotr_3, wekotr_4, wekotr_5, wekotr_6, wekotr_7, wekotr_8, wekotr_9, wekotr_10, wekotr_11];
% Sort vector..
[odl, sortOrder] = sort(Tablica);
najblizsze_kNN = odl(1:7)
Indeksy_kNN = sortOrder(1:7)
if all(odl(1:7) == odl(1))
% All of the first 7 values are the same.
w_1= 1/(1+(odl(1))^2)
w_2= 1/(1+(odl(2))^2)
w_3= 1/(1+(odl(3))^2)
w_4= 1/(1+(odl(4))^2)
w_5= 1/(1+(odl(5))^2)
w_6= 1/(1+(odl(6))^2)
w_7= 1/(1+(odl(7))^2)
W=[w_1, w_2, w_3, w_4, w_5, w_6, w_7]
[wartosc index_w]=min(W)
else
% Find index of smallest value in sorted vector.
[minValue, indexOfMinValue] = min(odl)
end

Sign in to comment.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!