Find a value using an index obtained from a different matrix with same dimensions
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Aquatris
2024-4-10
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.
[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.
采纳的回答
Aquatris
2024-4-10
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!