필터 지우기
필터 지우기

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

조회 수: 4 (최근 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
Pawel Szczepanowski
Pawel Szczepanowski 2021년 12월 2일
wektor_1, wektor_2, etc are the vecotors with values. Vector Tablica has 77 diffrent numbers

댓글을 달려면 로그인하십시오.

답변 (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
Pawel Szczepanowski 2021년 12월 2일
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