我正在开发一个MatLab程序,它计算数字的权力,将它们加在一起,然后查看是否有第一组数字(功率数字)等于任何添加数字的权力。我想检查此为第一阵列中的每个值,但是,我得到这样的输出:每个m
值如何检查一个数组中的任何变量是否在另一个数组中
m =
1
128
2187
16384
78125
279936
823543
2097152
4782969
10000000
,这是一个简单的for
循环数组的只是结果。所以当我去检查m
是否在数组中时,它在数组中检查是[1, 128,2187,16384,78125...]
,答案是否定的。我怎样才能得到它评估每个单个条目,比如:
Array n is [1,128,2187,16384]
for m = n
m = 1
Is m in array? No
m = 128
Is m in array? No
m = 2187
Is m in array? Yes
m = 16384
Is m in array? No
end
我的代码如下:
C = [];
D = [];
E = [];
F = [];
numbers1 = [];
numbers2 = [];
numbers = 10;
powers = 10;
for i = 1:numbers
for j = 3:powers
C = [C;i^j];
end
C = transpose(C);
D = [D;C];
C = [];
end
[~,b] = unique(D(:,1)); % indices to unique values in first column of D
D(b,:); % values at these rows
for i = D
for a = D
E = [E;i+a];
end
E = transpose(E);
F = [F;E];
E = [];
end
[~,b] = unique(F(:,1)); % indices to unique values in first column of F
F(b,:); % values at these rows
for m = D % this is the for loop mentioned above
m
end
你能更清楚地说明您的问题?而你的代码不能直接运行:'???未定义的函数或变量'number'。' –
@LuisMendo我试图更好地解释它,并且让代码可运行(忘记取出该行)。 – hichris123
而不是使用for循环,函数'find'可以是更好的主意否? – Vuwox