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.

回答(3 个)

Matt J
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 个评论
Blake
Blake 2024-9-3
Even by using this and the solution by Jatin Singh below has still both returned me with the message "rows = 0×1 empty double column vector."
Matt J
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
Jatin 2024-9-3
编辑:Jatin 2024-9-4
Hi @Blake,
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!

Walter Roberson
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)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by