matlab条件矩阵赋值 [英] matlab conditioned matrix assignment

查看:268
本文介绍了matlab条件矩阵赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对矩阵分配有疑问。

i have a question about matrix assignment.

说我有三个矩阵A,B和C,我想根据规则

say i have three matrices A, B and C, and i want to assign the elements of matrix C to the elements of A and B according to the rule

  C[i,j] = A[i,j] if abs(C[i,j] - A[i,j]) < abs(C[i,j] - B[i,j])
  C[i,j] = B[i,j] if abs(C[i,j] - A[i,j]) > abs(C[i,j] - B[i,j])
  C[i,j] = 0  if abs(C[i,j] - A[i,j]) == abs(C[i,j] - B[i,j])

如何在没有for循环的情况下编写它?

how can i write it without for loops?

非常感谢你的帮助。

推荐答案

我认为Dan Becker有正确的想法,但重新计算 abs(CB) abs(CA)意味着<比较em> updated 矩阵,而不是原始矩阵。

I think Dan Becker has the right idea, but re-computing abs(C-B) and abs(C-A) implies that the updated matrices are compared, not the original ones.

我不认为这是你想要的,所以这是他方法的更正版本:

I don't think this is what you want, so here's the corrected version of his method:

CmA = abs(C-A);
CmB = abs(C-B);

ind = Cma < CmB; C(ind) = A(ind);
ind = CmA > CmB; C(ind) = B(ind);
C(CmA == CmB) = 0;

这篇关于matlab条件矩阵赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆