How to find the closest value to a constant in a 2D matrix

104 ビュー (過去 30 日間)
HA
HA 2019 年 2 月 5 日
コメント済み: HA 2019 年 2 月 5 日
Hello,
I have a 2D matrix of random numbers and NaNs, of 15x129. I want to find where in the matrix the values are closest to the number 15 along each column, and output a logical 15x129 matrix (where number is closest to 15 = 1, where number is less than 15 = 0 or nan) . So out of the 15 values, which value is closest to the number 15.
Thank you,
Holly

採用された回答

YT
YT 2019 年 2 月 5 日
Probably something like this
A = rand([15 129])*100; %matrix with random values
[~,I] = min(abs(A - 15),[],1); %get indices for values closest to 15
logicalMat = false(size(A)); %preallocate logical matrix
arrayLength = size(A,2);
for jj = 1:arrayLength
logicalMat(I(jj),jj) = 1; %fill logical matrix
end
  1 件のコメント
HA
HA 2019 年 2 月 5 日
This works great, thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by