【问题标题】:Comparing arrays of different sizes without any loop在没有任何循环的情况下比较不同大小的数组
【发布时间】:2014-11-18 23:57:18
【问题描述】:

问题是这样的:

给定两个数组,a & b(均为正整数)。

special number 是一个数字,a(i) == i(值等于索引)。

如何检查数组 b 是否包含一个值是 special numbera

例如:a = [9 9 3 9]b = [3 4 5]。输出将是3。如果ba 为空,则输出为0。如果b 包含多个special number,则只会显示最小的一个。

这是我到目前为止所做的事情,无法从这里取得进展..

a = input('Please enter the array a : ');
b = input('Please enter the array b : ');

indexedArray = 1:length(a);
c = a-indexedArray;
t = find(c==0);   
p = find(t==b);

不工作。

顺便说一句:只能使用这些功能:.排序、 isempty 、 all 、 any 、 find 、 sum 、 max 、 min 、 长度。没有循环或条件!只允许使用数组。没有矩阵。不能使用 &、|

等逻辑运算符

谢谢!

【问题讨论】:

  • 这是作业吗? ab 是向量,还是可以是更高维的数组(矩阵等?)。
  • vectors.. 设法使它与“ismember”一起工作.. 但不能使用此功能。
  • (a和b是一维数组)
  • 这是一个非常人为的家庭作业问题。不能使用 cmets 中的任何特殊功能就像告诉某人只用石头和火做饭一样。拥有这些工具是 MATLAB 的独特之处。如果你不能使用它们,那么使用 MATLAB 就没有意义了。
  • @rayryeng:我同意。虽然它可能会教一些关于索引的知识(请参阅我的解决方案),但与其让人们做谜题,为什么不让他们做一个索引在现实问题中实际上有用的问题呢?

标签: arrays matlab compare


【解决方案1】:

我推迟发布我的解决方案,因为我 correctly 怀疑这个问题是家庭作业。不过,既然 OP 已经接受了Jonas's answer,那我还是贴一下我的吧。

代码

sumlengthanymin 的组合可以解决问题:

function out = stupidTutor(a, b)

a        = sum(a, 1);           % if a is empty, replace it by a 1-by-0 matrix
specials = a(a == 1:length(a)); % construct the vector of special numbers
b        = sum(b, 1);           % if b is empty, replace it by a 1-by-0 matrix

% some dyadic-product shenanigans
A   = specials' * (b == b);
B   = (specials == specials)' * b;
ind = any(A == B, 1);

temp = min(b(ind));         % temp is either a scalar, a 1-by-0 matrix, or []
out  = sum(sum(temp, 2), 1); % trick to return 0 in case temp be 1-by-0 or []

测试

%               a           b                 result
stupidTutor([9 9 3 9]  , [3 4 5])       %       3  
stupidTutor([9 9 3 9]  , [9 8])         %       0
stupidTutor([9 9 9 9 5], [3 4 5 3])     %       5
stupidTutor([9 9 3 9 5], [3 4 5 3])     %       3
stupidTutor([9 9 3 9 5], [5 4 3 2 1])   %       3
stupidTutor([9 9 3 9]  , [])            %       0
stupidTutor([]         , [3 4 5])       %       0
stupidTutor([]         , [])            %       0

【讨论】:

  • 我喜欢你的函数名 - +1。
  • @rayryeng 我从你的 cmets 中获得灵感 :)
  • 它最初并没有说明我的解决方案所具有的矩阵。 OP纠正了我并说他们是不允许的。自创建此帖子以来,他或她至少更改了 3 次规范。这可能是我对这个问题不屑一顾的原因......而且它完全是人为的。
  • @rayryeng ffs...我也放弃了这个问题。让它被否决。
  • @Jubobs - 完全同意我的朋友。
【解决方案2】:

好吧,事实证明毕竟有办法:)。我们利用了数字必须严格为正才能成为特殊数字的事实。

%# in case we need to handle empty inputs: replace empty input with 0 or 1, respectively.
a = sum(a(:)',1);
bIsEmpty = isempty(b);
b = sum(b(:)',1); b = max(b,1);

specialNumber = find(a==1:length(a));

maxAB = max(max(a), max(b));

%# "zeros()"
bigVectorForComparisonA = (1:maxAB)*0;
bigVectorForComparisonB = (1:maxAB)*0;

bigVectorForComparisonA(specialNumber) = 1;
bigVectorForComparisonB(b) = 1;

%# instead of &, we add. Find only the smallest match
specialNumberInB = find(bigVectorForComparisonA + bigVectorForComparisonB == 2,1,'first');

out = sum(specialNumberInB) * ~bIsEmpty; %# sum([]) = 0

对于一个稍微漂亮的解决方案,假设a 中最多有 1 个特殊数字

specialNumber = min(find(a==(1:length(a)));

out = any(b==specialNumber)*sum(specialNumber);

【讨论】:

  • +1 - 这基本上是我的想法,但是当我无法使用& 时我很生气。感谢您解决了这个可怕的问题。
  • 该问题包含以下规范:如果ba 为空,则输出为0。如果a 为空,您的方法将失败;你得到一个Matrix dimensions must agree 错误。
  • @Jubobs:我认为不需要处理空输入。无论如何,我可以解决这个问题。
  • unique() 不是我们被允许使用的东西。抱歉,这个问题让人头疼。顺便说一句,如果ab为空,则显示0
  • @Osh24 - 无意冒犯,但你的 MATLAB 导师很烂。他们应该教你如何有效地使用 MATLAB,而不是让你跳来跳去用 10 行代码做某事,而这可以在 2 中完成。这可能是我曾经尝试回答并阅读过的最令人沮丧的问题我在 StackOverflow 上回答问题的历史。一半是因为你忘记了需求,你不得不不断地修改你的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-13
  • 2016-11-05
  • 2015-02-24
  • 1970-01-01
  • 2021-11-18
  • 2019-09-23
相关资源
最近更新 更多