Matlab 中的逐元素 if - 它们存在吗?

发布于 2024-07-18 07:46:30 字数 300 浏览 11 评论 0原文

假设我有以下基本 if 语句:

if (A ~= 0)
   % do something like divide your favorite number by A
else
   % do something like return NaN or infinity
end

问题是 A 不是一个简单的数字,而是一个向量。 如果 A 中没有元素为 0,Matlab 返回 true。我正在寻找的是矢量化的? 为 A 中的每个元素执行上面的 if 语句的方法。

实际上,我只是想尽快执行此操作。

Say I have the following basic if-statement:

if (A ~= 0)
   % do something like divide your favorite number by A
else
   % do something like return NaN or infinity
end

The problem is that A is not a simple number but a vector. Matlab returns true if no element in A is 0. What I am looking for is a vectorized? way of perforimg the if-statement above for each element in A.

Actually, I simply want to do this as fast as possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心不设防 2024-07-25 07:46:30

向量化 if 不存在,但有一些选项。 如果要测试所有或任何元素是否为 true,请使用 all 或 any 函数。

以下是有条件地修改矩阵值的一个示例:

b = A ~= 0;      % b is a boolean matrix pointing to nonzero indices
                 % (b could be derived from some other condition,
                 %  like b = sin(A)>0
A(b) = f(A(b))   % do something with the indices that pass
A(~b) = g(A(~b)) % do something else with the indices that fail

Vectorized ifs don't exist, but there are some options. If you want to test for all or any elements true, use the all or any function.

Here's one example of conditionally modifying values of a matrix:

b = A ~= 0;      % b is a boolean matrix pointing to nonzero indices
                 % (b could be derived from some other condition,
                 %  like b = sin(A)>0
A(b) = f(A(b))   % do something with the indices that pass
A(~b) = g(A(~b)) % do something else with the indices that fail
热鲨 2024-07-25 07:46:30
B = zeros(size(A));
B(A~=0) = FAV./A(A~=0);  
B(A==0) = NaN;
B = zeros(size(A));
B(A~=0) = FAV./A(A~=0);  
B(A==0) = NaN;
若无相欠,怎会相见 2024-07-25 07:46:30

一般来说,要对矩阵的某些元素执行一项操作,而对其余元素执行另一项操作,一行解决方案是:

Z = B .* X + ~B .* Y;

其中 B 是逻辑矩阵。 例如,

Z = (A == 0) .* -1 + (A ~= 0) .* A;

复制 A,但在 A 为零的地方分配 -1。

然而,因为这个问题涉及无穷大或 NaN,所以可以更简洁地完成:

Z = FAV ./ A; % produces inf where A == 0
Z = (A ~= 0) .* FAV ./ A; % produces NaN where A == 0

In general, to perform one operation on some elements of a matrix and another operation on the remaining elements, a one-line solution is:

Z = B .* X + ~B .* Y;

where B is a logical matrix. As an example,

Z = (A == 0) .* -1 + (A ~= 0) .* A;

copies A but assigns -1 everywhere that A is zero.

However, because the question deals with infinity or NaNs, it can be done even more succinctly:

Z = FAV ./ A; % produces inf where A == 0
Z = (A ~= 0) .* FAV ./ A; % produces NaN where A == 0
止于盛夏 2024-07-25 07:46:30

您是否正在寻找所有非零元素? 您可以通过几种方式做到这一点。

nonzero = find(A); % returns indicies to all non-zero elements of A
y = x./A(nonzero); % divides x by all non-zero elements of A
                   % y will be the same size as nonzero

或者对于单行语句,您可以使用条件代替索引

y = x./A(A~=0); % divides x by all non-zero elements of A

Are you looking for all non-zero elements? You can do this a couple of ways.

nonzero = find(A); % returns indicies to all non-zero elements of A
y = x./A(nonzero); % divides x by all non-zero elements of A
                   % y will be the same size as nonzero

Or for a one-liner, you can use a conditional in place of indicies

y = x./A(A~=0); % divides x by all non-zero elements of A
草莓酥 2024-07-25 07:46:30

您需要做的是确定要操作的元素。 我会使用“查找”。 我将结果存储在 VI(有效索引)中,并使用它来填充矩阵。

clear
clc

den = [2 0 2; 0 2 0; -2 -2 -2]
num = ones(size(den));
frac = nan(size(den));

vi = (den ~=0)

frac(vi) = num(vi)./den(vi)

vi = (den == 0)

frac(vi) = nan %just for good measure...

What you need to do is identify the elements you want to operate on. I would use FIND. I store the results in VI (Valid Indicies) and use that to populate the matrix.

clear
clc

den = [2 0 2; 0 2 0; -2 -2 -2]
num = ones(size(den));
frac = nan(size(den));

vi = (den ~=0)

frac(vi) = num(vi)./den(vi)

vi = (den == 0)

frac(vi) = nan %just for good measure...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文