Find a value using an index obtained from a different matrix with same dimensions

56 views (last 30 days)
Hello, I am working with huge amount of data which require to find a specific index in a matrix using a condition, and then use the indexes to extract another value using another matrix with the same dimension. For example: Let us say that I am iterating the same calculation varying the angle in a specfic range: x=0:1:5 degrees and then calculate for each angle a specific case like this.
x=[0 1 2 3 4 5] angle matrix;
a1= [2 3 5 2 1 0] % one case scenario, each value it has been obtanined for each angles 0,1 2 3 4 5 as the matrix "x"
if I repeat the proces using different conditions like a2 and a3 with dfifferent angles
a2=[0 1 3 1 0 0];
a3=[3 2 1 0 0 0]
The total matrix "a" will be:
a=[a1;a2;a3]=[2 3 5 2 1 0;0 1 3 1 0 0;3 2 1 0 0 0];
if I want to find the maximun value for each case a1, a2 and a3 (or each row of a) and obtain the angles at which the maximun occurs I did the following:
Max_a=max(a,[],2);
ind=find(a==Max_a); or [row,column]=find(a==Max_a);
then I can use the index to locate the angle creating a new matrix of angles with the same length of matrix a according to the different cases like this:
x_total=[x;x;x];=[0 1 2 3 4 5;0 1 2 3 4 5;0 1 2 3 4 5]
Then, I can use the indexes (ind or row and column) to extract the angles at which the maximum occurs for each a1, a2 and a3 with the following:
x_maxa = a(ind); or ind1 = sub2ind(size(x), row, column); x_maxa(ind1);
x_maxa=[2;2;0];
The problem is when I used huge amount of data like varying angles from 0:1:180 and cases from 1 to 100, I realized that these commands do not provide an accurate estimations of the angles at which the maximum occurs for each case. I would appreciate the help. I provide as well two txt.files where you find the total angle matrix and total values matrix for my real case.
  3 Comments
Jorge Luis
Jorge Luis on 10 Apr 2024 at 13:20
Hi, thank you for your response. The code is like this
% loading data
TableRotDpp_SA_NR= load('Total_matrix_cases.txt');
TableRotDpp_Rot_NR= load('Total_matrix_rotations.txt');
Max_RotD_SA=max(TableRotDpp_SA_NR,[],2);
idx_Max_SA = find(TableRotDpp_SA_NR==Max_RotD_SA);
Max_Rotation_RotD_SA = TableRotDpp_Rot_NR(idx_Max_SA);
Kind regards
Aquatris
Aquatris on 10 Apr 2024 at 14:28
The issue in your code was I think, it was not giving you the correct values for each row. So your expectation was Max_Rotation_RotD_SA would represent the max values of each row, but it was not.
If you make this simple change as @Stephen23 showed in his answer, your code also works properly:P
[Max_RotD_SA,idx_Max_SA]=max(TableRotDpp_SA_NR,[],2,'linear');
But @Jorge Luis is correct that this code only checks a single instance of max values and neglects other values that might also exist in the same row that result in the same max value for that row.

Sign in to comment.

Accepted Answer

Aquatris
Aquatris on 10 Apr 2024 at 11:00
Something like this maybe:
%% your matrices
val = load('Total_matrix_cases.txt');
x = load('Total_matrix_rotations.txt');
% find maximum values in val for each row
[valMax,idx] = max(val,[],2); % idx is column index for each row
s = [];
for i = 1:size(val,1)
s(i,:) = [i x(i,idx(i)) val(i,idx(i)) valMax(i)];
end
% s = [rowNumber x val(x) valMax] for each row
disp(s)
1.0000 17.0000 10.6700 10.6700 2.0000 17.0000 11.6398 11.6398 3.0000 152.0000 16.2679 16.2679 4.0000 89.0000 25.3473 25.3473 5.0000 50.0000 42.3446 42.3446 6.0000 154.0000 39.9599 39.9599 7.0000 159.0000 40.2815 40.2815 8.0000 125.0000 47.3734 47.3734 9.0000 82.0000 34.9502 34.9502 10.0000 57.0000 34.9621 34.9621 11.0000 107.0000 7.4710 7.4710 12.0000 113.0000 5.8002 5.8002 13.0000 126.0000 2.3651 2.3651 14.0000 124.0000 1.2982 1.2982 15.0000 96.0000 0.7132 0.7132 16.0000 95.0000 0.5445 0.5445 17.0000 68.0000 0.5043 0.5043 18.0000 45.0000 0.4373 0.4373 19.0000 55.0000 0.3334 0.3334 20.0000 50.0000 0.2636 0.2636

More Answers (1)

Stephen23
Stephen23 on 10 Apr 2024 at 12:17
Edited: Stephen23 on 10 Apr 2024 at 12:25
The efficient MATLAB approach:
tmc = readmatrix('Total_matrix_cases.txt');
tmr = readmatrix('Total_matrix_rotation.txt');
[tmx,idx] = max(tmc,[],2,'linear');
nmr = 1:numel(tmx);
out = [nmr(:),tmr(idx),tmx]
out = 20x3
1.0000 17.0000 10.6700 2.0000 17.0000 11.6398 3.0000 152.0000 16.2679 4.0000 89.0000 25.3473 5.0000 50.0000 42.3446 6.0000 154.0000 39.9599 7.0000 159.0000 40.2815 8.0000 125.0000 47.3734 9.0000 82.0000 34.9502 10.0000 57.0000 34.9621
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!