How to read ranges

1 visualización (últimos 30 días)
Kanakaiah Jakkula
Kanakaiah Jakkula el 5 de Sept. de 2015
Respondida: Geoff Hayes el 5 de Sept. de 2015
Table1
Range Rank
100~90 10
<90~<=80 9
<80~<=70 8
<70~<=60 7
<60~<=50 0
<50~<=40 0
<40~<=30 0
<30~<=20 0
<20~<=10 0
<10~0 0
Table2:
90 0 80
100 100 20
10 5 1
I want to compare Table2 based on Table1 constraints. I want to compare each element in Table2 falls which catagory of Table1. For example in table2 first row, first column element is 90, it falls in the catagory of 100~90, and coresponding rank is 10, so I shd store in output table first row, first column is 10. So, my resulting output (Table3)as below:
10 0 9
10 10 0
0 0 0
Kindly help me, how can I do this. Many many thanks in advance.

Respuestas (1)

Geoff Hayes
Geoff Hayes el 5 de Sept. de 2015
Kanakaiah - I would perhaps take your first table and re-build so that you have three columns: the first being the upper bound of the interval, the second being the lower bound of the interval, and the third being the rank for that interval. Something like
intervalsWithRanks = [
100 90 10
90 80 9
80 70 8
70 60 7
60 50 0
50 40 0
40 30 0
30 20 0
20 10 0
10 0 0];
Then, using your data initialized as
data = [
90 0 80
100 100 20
10 5 1];
we can loop through each element of data and find which rank should be applied. Something like
rankedData = zeros(size(data));
maxUpperBound = intervalsWithRanks(1,1);
minLowerBound = intervalsWithRanks(end,2);
for u=1:size(data,1)
for v=1:size(data,2)
val = data(u,v);
if val <= maxUpperBound && val >= minLowerBound
idx = find(intervalsWithRanks(:,2)>=data(u,v),1,'last');
if ~isempty(idx)
rankedData(u,v) = intervalsWithRanks(idx,3);
else
rankedData(u,v) = intervalsWithRanks(1,3);
end
end
end
end
Note how we use the two for loops to iterate over each element in the data matrix (there are probably more efficient algorithms for doing this, but this just gives a basic approach) and then, using find), look for that index (row) of intervalsWithRanks for all rows whose second column (the lower bound) is less than or equal to the element of interest (for example, 80). Since both the first two lower bounds are 90 and 80, then two indices are returned from find of which we are interested in only the smaller of the two. Since our lower bounds are in a decreasing order, then we use can use last to get the second row index, idx which we use within the third column of intervalsWithRanks to get the rank for that element.
Try running through the code and step through each line (using the debugger) to get an idea of how it is working and whether it suits your needs.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by