Simply trying to find a value in the same row as my known value but in a different column.
3 次查看(过去 30 天)
显示 更早的评论
I have a 225x3 matrix and I have a known value somewhere in column 1. How do I find the value that is in the same row as my known value but in column 3? Everything I have tried also gives me an answer of "0×1 empty double column vector," but without any value.
I would also like to know how to simply just find what row my value is in, but everything I have tried for that also gives me the same "0×1 empty double column vector" answer.
0 个评论
回答(3 个)
Matt J
2024-9-3
编辑:Matt J
2024-9-3
Everything I have tried also gives me an answer of "0×1 empty double column vector," but without any value.
You haven't shown us what you've tried, but my guess is that you've ignored floating point precision errors when testing for equality with your value. Calling your matrix A, one solution might be,
rows=find(abs(A(:,1)-value)<1e-6);
A( rows, 3)
2 个评论
Matt J
2024-9-3
Well, attach the matrix in a .mat file and run your code for us. We still have no visual picture what you are doing or what your data looks like..
Jatin
2024-9-3
编辑:Jatin
2024-9-4
If you want to obtain the index of a specific value in your matrix, you can use the "find" function, which returns the indices of each entry that matches the specified value.
In your case you can use the below code to get the indices of values matching with "value" in column 1 of a matrix 'A'.
Indices = find(A(:,1) == value);
To get the corresponding values in column 3 in the matrix you can use:
col3Values = A(Indices, 3);
Please note that the "find" function returns a list of indices for all matching values. If you want to obtain only the first or last occurrence of a value, you can specify the direction using the 'first' or 'last' keyword.
kindly follow the documentation below for more information on "find" function:
Hope this helps!
0 个评论
Walter Roberson
2024-9-3
rows = ismembertol( A(:,1), value);
A( rows, 3)
If this does not work, then either A(:,1) is not the same datatype as value, or else value does not appear approximately in A(:,1)
0 个评论
另请参阅
类别
在 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!