Print the first seven values from the sorted vector with value and index to the new vector.
조회 수: 1 (최근 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
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
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 Center 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!