match elements of a matrix to an array

2 ビュー (過去 30 日間)
SChow
SChow 2020 年 11 月 25 日
編集済み: Ameer Hamza 2020 年 11 月 25 日
Hello,
I have one matrix (A1) of size 4000x8000 and the second matrix (g1) of size 100000x2.
I am trying to match each number in A1 to the nearest number in g1(:,1) and build another matrix (xxx) witn the corresponding element in g1(:,2).
I am tring the following however I get an error
Error using bsxfun
Requested 50001x37324800 (6952.4GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause
MATLAB to become unresponsive.
The following process works for smaller matrices.
Any help is much appreciated.
g1=readtable('interp.csv');
g=g1{:,:};
s=A1(:)';%%converting A1 to n array
bsxfun(@minus,g(:,1),s);
[~,minRow] = min(abs(bsxfun(@minus,g(:,1),s)));
xxx = [ s.', g(minRow,2:end) ];

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 11 月 25 日
編集済み: Ameer Hamza 2020 年 11 月 25 日
A much easier solution is to use interp1() with 'nearest' option
xxx = interp1(B(:,1), B(:,2), A, 'nearest')
  2 件のコメント
SChow
SChow 2020 年 11 月 25 日
Wonderful! Thanks!!
Much easier,
Ameer Hamza
Ameer Hamza 2020 年 11 月 25 日
I am glad to be of help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by