필터 지우기
필터 지우기

Change values of a matrix if meet the condition

조회 수: 1 (최근 30일)
Andres Serrano
Andres Serrano 2018년 11월 2일
편집: James Tursa 2018년 11월 2일
Hi guys a have a 2*2 matrix. and I want to substite the values of the 2 columns based in 2 different conditions. EX a= '1' '0' '0' '1'. For the first column if X=1 y=0.6 and if X=0 y=0.4. Similarly in column 2 if x=1 y=0.7 and if x=0 y=0.3. Therefore, new matrix b= '0.6' ' 0.4' '0.3' '0.7'.
  댓글 수: 2
madhan ravi
madhan ravi 2018년 11월 2일
Please edit your question so that it’s easy to read
Andres Serrano
Andres Serrano 2018년 11월 2일
Hi Madhan, I have attached a script

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

답변 (1개)

James Tursa
James Tursa 2018년 11월 2일
편집: James Tursa 2018년 11월 2일
E.g., I think this is what you are asking for:
a = your matrix of 1's and 0's
p = your matrix of values (1st row corresponding to 0 values, 2nd row corresponding to 1 values)
c = 1:size(a,1):numel(a); % Set up for linear indexing
b = a; % An arbitrary matrix the same size as a
b(:) = p(bsxfun(@plus,a,c)); % Replace elements with appropriate p values (linear indexing)
On later versions of MATLAB you don't need bsxfun:
b(:) = p(a+c); % Replace elements with appropriate p values (linear indexing)
A sample run:
>> a = eye(2)
a =
1 0
0 1
>> p = [0.4 0.3; 0.6 0.7]
p =
0.4000 0.3000
0.6000 0.7000
>> c = 1:size(a,1):numel(a)
c =
1 3
>> b = a
b =
1 0
0 1
>> b(:) = p(bsxfun(@plus,a,c))
b =
0.6000 0.3000
0.4000 0.7000

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by