在 matlab/octave 中查找不在数组中的值

发布于 2024-12-09 01:53:44 字数 261 浏览 0 评论 0原文

我在 matlab/octave 中有两个数组,计算 a1 并给出 a2 。如何创建第三个数组 a3 比较 a1 和 a2 并显示 a1 中缺少的值?

a1=[1,4,5,8,13]
a2=[1,2,3,4,5,6,7,8,9,10,11,12,13]
a3=[3,6,7,9,10,11,12]

如果a1=[1,4,5,8.6,13],这也适用于浮点数,或者我必须将 a1 只转换为整数。

谢谢

I have two arrays in matlab/octave a1 is calculated and a2 is given. How can I create a 3rd array
a3 that compares a1 to a2 and shows the values that are missing in a1?

a1=[1,4,5,8,13]
a2=[1,2,3,4,5,6,7,8,9,10,11,12,13]
a3=[3,6,7,9,10,11,12]

Also can this work for a floating point number say if a1=[1,4,5,8.6,13] or would I have to convert a1 to integers only.

Thanks

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

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

发布评论

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

评论(2

只是偏爱你 2024-12-16 01:53:44

setdiff 返回 1 的元素不在另一个数组中的数组。这适用于浮点值,但需要相等。

a3 = setdiff(a2, a1)

setdiff returns the elements of one array that aren't in another. This will work with floating-point values, but requires equality.

a3 = setdiff(a2, a1)
毁我热情 2024-12-16 01:53:44
function missing = comparray(a1, a2)
% array of numbers that are missing from input
missing = []
% for each element in a2, check if it's in a1
for ii=1:1:length(a2)
    num = a2(ii);
    deltas = abs(a1 - num);
    if min(deltas) ~= 0
        missing = [missing, num];
    end
end

浮点数可能很棘手。要使上述代码与它们一起使用,请检查 min(deltas) > 0.001(或者考虑到输入数字的精度,一个合适的非常小的值)。有关详细信息,请参阅此处< /a>

function missing = comparray(a1, a2)
% array of numbers that are missing from input
missing = []
% for each element in a2, check if it's in a1
for ii=1:1:length(a2)
    num = a2(ii);
    deltas = abs(a1 - num);
    if min(deltas) ~= 0
        missing = [missing, num];
    end
end

Floating point numbers can be tricky. To get the above code to work with them, check min(deltas) > 0.001 (or a suitable very small value given the precision of your input numbers). For more information, see here

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