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

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

We can't run images, only text code. To write out stuff to the command window, use fprintf(). To sort, use sort().
Can u see?
Tablica = ([wekotr_1, wekotr_2, wekotr_3, wekotr_4, wekotr_5, wekotr_6, wekotr_7, wekotr_8, wekotr_9, wekotr_10, wekotr_11]);
index = []; %indeks
odl=[]; %odlegloci
[odl,index] = sort(Tablica);
najblizsze_kNN = odl(1:7)
Indeksy_kNN = index(1:7)
if odl(1)==odl(2)==odl(3)==odl(4)==odl(5)==odl(6)==odl(7)
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
Najmniejsza_odl=min(najblizsze_kNN)
Klasa=Indeksy_kNN(Najmniejsza_odl)
end
Yeah, but we can't run it:
Unrecognized function or variable 'wekotr_1'.
What are all those values?
wektor_1, wektor_2, etc are the vecotors with values. Vector Tablica has 77 diffrent numbers
and vector Tablica is consist of wektor_1, wektor_2, etc

Sign in to comment.

Answers (1)

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

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)
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

Products

Asked:

on 2 Dec 2021

Commented:

on 2 Dec 2021

Community Treasure Hunt

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

Start Hunting!