How do I pull data from two different matrices to form a new matrix?

Hi,
I have two different 30 x 10 matrixes (d1 and d2) that I am trying to pull rows from and only want a specific row from each of them to form a new matrix. The rows that I want are specified in another 30x1 matrix (I) with values of only 1's and 2's depicting the matrix I need to pull from.
I know i could pull one row of a specified matrix with the following command:
dtest=d1(I(1),:)
Unfortunately, I want to use my I matrix to select d1 or d2 in the above command.

 채택된 답변

d12 = cat(3, d1, d2);
[r, c] = size(d1);
dtest = d12( sub2ind(size(d12),repmat((1:r).',1,c),repmat(1:c,r,1),repmat(I,1,c)) );

추가 답변 (1개)

Roger Stafford
Roger Stafford 2016년 11월 20일
편집: Roger Stafford 2016년 11월 21일
p = repmat(I==1,1,10); <-- Corrected
D = p.*D1+(1-p).*D2; % <-- The desired result

댓글 수: 2

I think your repmat() is off there; with a single count you are asking for repeating 10 horizontally and 10 vertically.
Oops, you're right Walter. It should be
p = repmat(I==1,1,10);

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

질문:

2016년 11월 20일

편집:

2016년 11월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by