How to extract matching ranks

2 views (last 30 days)
Mekala balaji
Mekala balaji on 5 Sep 2015
Answered: Geoff Hayes on 6 Sep 2015
I have matrix A &B. I want to find rank of elements in B based on matrix A. For example B(1,1)=100. So, it falls in the range of 100 i.e, A(1,10). The corresponding rank is 10. The output(1,1)=10. The B(1,2)=50, the range is A(5,1), the corresponding rank is 0. output(1,2)=0, and B(1,3)=100, it falls in the range of A(1,10), the corresponding rank=10. The out (1,3)=10 so on. My code is below:
clc;
clear all;
close all;
T=[10 0;
20 0;
30 0;
40 0;
50 0;
60 0;
70 7;
80 8;
90 9;
100 10];
B=[100 50 100;12 40 10];
match=[];
for i=1:size(B,2)
for j=1:size(B,1)
testval(j,i) = B(j,i);
% row(i,j) = T(find((T(:,1) > testval(i,j)) == 1, 1, 'first'), 2:end);
row(j,i) = T(find((T(:,1) > testval(j,i)) == 1, 1, 'first'), 2);
Output(j,i)=[match row(j,i)];
end
end
I encountered the following error:
??? Subscripted assignment dimension mismatch.
Error in ==> ScoresTable at 21
row(j,i) = T(find((T(:,1) > testval(j,i)) == 1, 1, 'first'), 2);
Kindly help me, how to solve this.
Many thanks in advance.

Answers (1)

Geoff Hayes
Geoff Hayes on 6 Sep 2015
Mekala - before trying to understand what is causing the problem, you may want to do a couple of things first to simplify your code. There is no need to create a testval matrix which is updated on each iteration. Instead, just assign a scalar to the value extracted from your matrix B as
testval = B(j,i);
Second, using i and j as indexing variables is not always a good idea since MATLAB uses i and j as representations of the imaginary number. So avoid confusion (and possible errors), consider naming your variables as something different. And third, rather than updating your Output and match and row arrays, just simplify to Output and pre-size it before you enter the outer for loop. This would reduce your code to
B=[100 50 100;12 40 10];
ranksForB = zeros(size(B));
for i=1:size(B,2)
for j=1:size(B,1)
testval = B(j,i);
ranksForB(j,i) = T(find((T(:,1) > testval) == 1, 1, 'first'), 2);
end
end
So there is still an error in the above, so the must useful thing to do is to debug the code. Run the command
dbstop if error
so that when you execute the above script, the MATLAB debugger will pause at the line where the error has occurred and you can look to see what is wrong. In this case, the result of
find((T(:,1) > testval) == 1, 1, 'first')
is an empty matrix since testval is 100 and there is no element within the first column of T that is greater than 100. So you need to change your condition to greater than or equal to and capture the case where the index returned by the above find is empty. Your code would then become
B=[100 50 100;12 40 10];
ranksForB = zeros(size(B));
for i=1:size(B,2)
for j=1:size(B,1)
testval = B(j,i);
idx = find(T(:,1) >= testval, 1, 'first');
if ~isempty(idx)
ranksForB(j,i) = T(idx, 2);
end
end
end
Note how I have removed the ==1 as it is unnecessary. The answer for ranksForB is then
ranksForB =
10 0 10
0 0 0
which should be the expected result given your B.
Try the above and see what happens!

Community Treasure Hunt

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

Start Hunting!