# How to find indices of certain values from dataset using a loop

19 views (last 30 days)
Iliqe on 23 Jan 2021
Commented: Iliqe on 25 Jan 2021
Hi gyus, I am a beginner in coding.
My problem is that I want to find indices of values using a loop, but having and error.
Could anyone explain what is wrong with loop and give a solution??? (file which I use is attached)
The general idea is to find 5 smallest values and its indices from the dataset (column "Lag").
clear;
newarray = sort(data(:,5));
k=5;
values = newarray(1:k);
for i=1:k
index = find(values==data(:,5));
end

Andreas Apostolatos on 23 Jan 2021
Hello,
You can use the following code snippet for your purposes:
clear;
data = readtable('kNN_300_y_pred_wd XYZValue + lag.csv');
newarray = sort(data{:,5});
k=5;
values = newarray(1:k);
for i=1:k
[~, indices] = ismember(values, data{:,5});
end
I hope that helps.
Kind Regards,
Andreas
##### 3 CommentsShow 1 older commentHide 1 older comment
Andreas Apostolatos on 24 Jan 2021
Edited: Andreas Apostolatos on 24 Jan 2021
Hello Iliqe,
My first answer was meant to address your issue about the errors you were receiving. The reason of the error was that you were trying to read-in a csv-file using function 'xlsread', which was returning an empty array that you were then trying to index. Please use function 'xlsread' to read-in xls-files.
Function 'ismember' returns the index of the first occurrence of each of the elements of the array whose elements are sought in the second array. In this case your column "Lag" has multiple occurrences of some components, such as component with value 1 and component with value 2. Therefore, function 'ismember' returns only the indices of the first occurrence of these components in your column.
From your description I believe that you would like to have all the indices of the corresponding components that appear multiple times. I would recommend the following code snippet for your purpose,
clc, clear;
data = readmatrix('kNN_300_y_pred_wd XYZValue + lag.csv');
% number of smallest numbers that are sought
num = 5;
% get the num-smallest numbers from the 5-th column of array data (Lag)
smallest_five_values_lag = sort(data(:, num));
smallest_five_values_lag = smallest_five_values_lag(1:5);
% initialize the array of indices
indices = zeros(num, 1);
% initialize counter
count = 1;
% loop over all the num-smallest numbers
for i = 1:num
% check for multi-occurrence of a component
if i > 1
if smallest_five_values_lag(i, 1) == smallest_five_values_lag(i - 1, 1)
count = count + 1;
else
count = 1;
end
end
% find the indices of the component in the 5-th column of array data (Lag)
idx = find(data(:,5) == smallest_five_values_lag(i, 1));
% place the found index in the array of indices appropriately
indices(i, 1) = idx(count);
end
Note that I herein used function 'readmatrix' to read-in csv-file directly into a numeric array instead of a table, as you already know the column that you would like to use, namely the 5th one "Lag".
You can verify that these are indeed the correct indices by means of the following command,
components = data(indices, 5)
which returns,
components =
0
1.0000
1.0000
1.4142
2.0000
These are indeed the 5 smallest elements of the 5-th column of array data, namely of the column with name "Lag".
Indeed MATLAB arrays are indexed starting from 1 and not from 0.
Kind Regards,
Andreas
Iliqe on 25 Jan 2021
Many thanks to you Andreas.