New matrix creation based on 2 existing matrices

I have 2 matrices, a binary matrix representing data points (A) and the other with colour values for each trial (B). Both matrices are equal size. What I am trying to do is create a new matrix that contains the colour value from (B) wherever there is a 1 in (A) and NaN wherever there is a 0.
I am having trouble figure out how to properly map the two matrices together though. I have tried putting C(k,:) = B(k, A==1) in a loop but get an error exceeding dimensions. Also thought C = B(A) might work but it just creates a single column matrix with all the colours in question. I need to the matrix to match the dimensions of the pre-existing matrices.

댓글 수: 1

Can you provide sample inputs and the expected results?
Thanks!

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

 채택된 답변

Star Strider
Star Strider 2014년 11월 12일

0 개 추천

You’re close, but logical indexing needs to have both sides of the equation have matching indices to work.
This example will do what you want:
A = randi([0 1],10,10)
B = randi([100 107], 10,10)
C = NaN(10);
C(A==1) = B(A==1)
It assumes your colour value is a single scalar quantity. Here, I have numbered the colours [100:107].

댓글 수: 2

Nick
Nick 2014년 11월 14일
Awesome, thanks a lot.
My pleasure!

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

추가 답변 (0개)

카테고리

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

질문:

2014년 11월 12일

댓글:

2014년 11월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by