在matlab中比较两个相同长度的不同向量

发布于 2024-12-17 22:44:58 字数 451 浏览 2 评论 0原文

我有两个矩阵 A 和 B。A 是一个长度为 1000X1 的数组,由 0 和 1 组成,B 也具有相同的大小。现在 B 是一个以 A 作为输入执行扫描操作的函数的输出。扫描只不过是在 A 中的 1 位置添加零。因此,B 的值与 A 类似,只是其中一些 1 被替换为 0。现在我的任务是填充 B 中的那些 0 值,这些值在扫描操作后被随机二进制值替换。我是在这个很棒的网站的用户的帮助下完成的。相同的代码在这里

idx=strfind(B,[0 0 0 0]);
n=dec2bin(randi([0 15],[numel(idx) 1]),4) - '0';
idx=bsxfun(@plus, idx', (0:3));
xx = B;
xx(idx(:)) = n(:);

现在这个函数还将替换 A 中已经存在的零。我只想替换那些从 1 更改为 0 的零,而不替换 A 中已经为零的那些 0。任何帮助我们将非常感激。

I have two matrix A and B. A is a array of length 1000X1 consisting of 0's and 1's and B is also of the same size. Now B is the output of a function that performs a sweep operation taking A as input. Sweep is nothing but addition of zeros in the place of 1 in A. so values of B is similar to A except some of the 1's are replaced by 0's. Now my task is to fill those 0 values in B which was replaced after sweep operation by random binary values.I have done this with help of a user form this awesome website. And the code for the same is here

idx=strfind(B,[0 0 0 0]);
n=dec2bin(randi([0 15],[numel(idx) 1]),4) - '0';
idx=bsxfun(@plus, idx', (0:3));
xx = B;
xx(idx(:)) = n(:);

Now this function will also replace the zeros which was already present in A. I want to replace only those zeros which were changed from 1's to 0's and not those 0's which were already zeros in A. Any help would be very much appreciated.

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

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

发布评论

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

评论(1

微暖i 2024-12-24 22:44:58

我会这样做:

function modifiedB=modifyB(A,B)
  idx=(A~=B); %% or idx=((A==1)&(B==0)); %% find the indices where B changed
  B(idx)=(rand(nnz(idx),1)>0.5); %% replace at those indices by random numbers
  modifiedB=B;
end

I would do:

function modifiedB=modifyB(A,B)
  idx=(A~=B); %% or idx=((A==1)&(B==0)); %% find the indices where B changed
  B(idx)=(rand(nnz(idx),1)>0.5); %% replace at those indices by random numbers
  modifiedB=B;
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文