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

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

 Respuesta aceptada

madhan ravi
madhan ravi el 26 de Jul. de 2019

1 voto

C = A==B

4 comentarios

madhan ravi
madhan ravi el 26 de Jul. de 2019
Imayan
Imayan el 26 de Jul. de 2019
ありがとうございます。
Cがロジックになります。等しい時に50、等しくない時に40を入れるような場合はできないでしょうか。
madhan ravi
madhan ravi el 26 de Jul. de 2019
Editada: madhan ravi el 26 de Jul. de 2019
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 el 26 de Jul. de 2019
Thank you very much.
ありがとうございます!
理解できました。

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2019a

Etiquetas

Preguntada:

el 26 de Jul. de 2019

Comentada:

el 26 de Jul. de 2019

Community Treasure Hunt

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

Start Hunting!