Pythonのnp.​whereと同じ動作​をつくりたい

조회 수: 16 (최근 30일)
Imayan
Imayan 2019년 7월 26일
댓글: Imayan 2019년 7월 26일
Pythonのライブラリにnumpyがあります。
import numpy as np
同サイズの行列(二次元の配列)で中身の異なるA,B,Cがあるとき、配列要素ごとの比較を行い、等しい時1、等しくなかったら0をCの各要素に入力したい。
C = np.where( A==B,1,0 );
上記のように書くと、Cに1と0が入った行列ができます。これをMATLABで作るにはどのような方法があるのでしょうか。

채택된 답변

madhan ravi
madhan ravi 2019년 7월 26일
  댓글 수: 4
madhan ravi
madhan ravi 2019년 7월 26일
편집: madhan ravi 2019년 7월 26일
Example:
A = [1:3;4:6;7:9];
B = [10,2,3;3,5,6;10,10,2];
idx = A==B;
C = zeros(size(A));
C(idx) = 50;
C(~idx) = 40
% if you're able to understand the above then it can be reduced to one line
C = 50 * (A==B) + 40 * (A~=B)
Imayan
Imayan 2019년 7월 26일
Thank you very much.
ありがとうございます!
理解できました。

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB の Python ライブラリ에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!