Automatically identify two numbers within an matrix

Is there any way to automatically locate two numbers within a matrix?
For example:
  • for the file "matrix_1.txt" I am interested in identifying the following numbers 194 (min) and 201 (max)
  • for the file "matrix.txt" I am interested in identifying the following numbers 175 (min) and 199 (max)
So I am interested in a code that can identify these numbers without me knowing them. Like the max/min command that identifies the max/min within a matrix for example.

5 件のコメント

Antonios Dougalis
Antonios Dougalis 2023 年 2 月 16 日
編集済み: Antonios Dougalis 2023 年 2 月 16 日
Hi Alberto, I am not sure if I have understood entirely your question.
If you are looking for any number in your array use the command 'find'. It uses logical indexing to identify the linear index of the number you want
e.g.,
if your matrix above is called A the following line will result the positional index of the the value your are seeking
result_min = find(A==175);
you can then convert it to array subscript with the command ind2sub
[row,col]=ind2sub(size(A),result_min)
Alberto Acri
Alberto Acri 2023 年 2 月 16 日
No, because the values I reported I have not known. So if I have matrix_1, I want to get as output the values 194 and 201.
Stephen23
Stephen23 2023 年 2 月 16 日
@Alberto Acri: apparently you do not want to consider small values in the matrices, e.g. 0, 1, 7, 11.
What is the limit for considering or ignoring the small values?
Alberto Acri
Alberto Acri 2023 年 2 月 16 日
Definitely ignore numbers smaller than 70.
John D'Errico
John D'Errico 2023 年 2 月 16 日
You are not telling us enough information. You want to find a number, like the max and min, But they may not be the smallest or largest numbers in the matrix. In fact, they are not even close to that.
So apparently you know what you want, but cannot describe it. And the matrices you show are not at all clear in how you would decide on those specific values.

サインインしてコメントする。

 採用された回答

Voss
Voss 2023 年 2 月 16 日
編集済み: Voss 2023 年 2 月 16 日

0 投票

matrix = readmatrix('matrix.txt');
matrix_1 = readmatrix('matrix_1.txt');
min_matrix = min(matrix(matrix >= 70))
min_matrix = 174
max_matrix = max(matrix(matrix >= 70))
max_matrix = 199
min_matrix_1 = min(matrix_1(matrix_1 >= 70))
min_matrix_1 = 194
max_matrix_1 = max(matrix_1(matrix_1 >= 70))
max_matrix_1 = 201

その他の回答 (1 件)

Mathieu NOE
Mathieu NOE 2023 年 2 月 16 日
編集済み: Mathieu NOE 2023 年 2 月 16 日

0 投票

hello Alberto
welcome back !
here a demo for the 'matrix.txt' case
the other one is basically the same , simply change the min / max values
M = readmatrix('matrix.txt');
value_min = 175;
value_max = 199;
ind = find(M>=value_min & M<=value_max); % linear index
M(ind) % show the M values (to double check)
[r,c] = ind2sub(size(M),ind) % show corresponding row / col indexes

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

リリース

R2021b

質問済み:

2023 年 2 月 16 日

編集済み:

2023 年 2 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by