フィルターのクリア

How to: map selected entries of one matrix to entries of a second?

3 ビュー (過去 30 日間)
Anthony Azevedo
Anthony Azevedo 2018 年 4 月 2 日
コメント済み: Anthony Azevedo 2018 年 4 月 2 日
I have a matrix, A, of 1024x1280 elements(an image), and I have two mapping matrices, Row_map (670 X 1428) and Col_map (670 X 1428), that take element Row_map(1,1), Col_map(1,1) of A (i.e. element A(4,892) where Row_map(1,1)=4, Col_map(1,1)=892) and put that value into matrix R(1,1), where elements of R are a rotated subset of values in A. R is also 670 X 1428, and some entries of Row_map, Col_map and R are nans. (Specifically, it's just the corner of the image, rotated an arbitrary angle, but I think that's beside the point). Once I have this mapping, I will go through a series of matrices like A (a video), and perform the same mapping.
Currently I'm doing the silly thing of looping over row and column indices of Row_map and Col_map to enter each value of R. There has to be a more efficient way, but I'm blanking right now. Really appreciate your help.

採用された回答

Stephen23
Stephen23 2018 年 4 月 2 日
編集済み: Stephen23 2018 年 4 月 2 日
Remove the NaN values, then use sub2ind:
isn = isnan(Row_map) | isnan(Col_map);
R = nan(size(isn));
idx = sub2ind(size(A),Row_map(~isn),Col_map(~isn));
R(~isn) = A(idx);
idx is the mapping that you need, into the non-NaN elements of R given by ~isn.
  1 件のコメント
Anthony Azevedo
Anthony Azevedo 2018 年 4 月 2 日
Very nice. Thanks, I was unaware of sub2ind. I appreciate both of your help.

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

その他の回答 (1 件)

KSSV
KSSV 2018 年 4 月 2 日
Read about sub2ind, ind2sub.
R = NaN(size(Row_map)) ;
idx = sub2ind(size(A), Row_map(:),Col_map) ;
R(idx) = A(idx) ;
  1 件のコメント
Stephen23
Stephen23 2018 年 4 月 2 日
編集済み: Stephen23 2018 年 4 月 2 日
This will not work: the indices in A (size 1024x1280) are clearly going to be different to those in R (size 670x1428).

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by