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

2 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2021-12-2
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 个评论
Pawel Szczepanowski
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 2021-12-2
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by