如果不相等,则比较多个数组的长度并显示错误

时间:2014-05-06 13:39:33

标签: matlab compare string-length

我有多个实数数组。 例如

a = [1:5]
b = [11:15]
c = [100:105]
d = [200:210]

如果数组的数量是100,我想要更短的代码。我所知道的是,

if length(a) == length(b) && length(b) == length(c) && length(c)== length(d)
else
error ('Length of data series is unequal. Correct it.')
end

如果所有系列不相等,如果长度不正确,则显示错误但是当数组编号和大小非常大(100)时,如何在小代码中完成? 谢谢。

1 个答案:

答案 0 :(得分:3)

<强>代码

%%// Get all the arrays/vectors into a cell array, with each element in each cell
A = [{a},{b},{c},{d}]

%%// Get the number of elements of all the cells, that is number of elements
%%// of all vectors into an array. This, this array could be thought of as an
%%// array of sizes
numels = cellfun(@numel,A)

%%// Now that we have an array of sizes, we need to make sure that 
%%//all the sizes are same. So when we compare all sizes to 
%%// any randomly picked one (first one in this case), must be matching, 
%%// which is being covered by the command - all.
if ~all(numels==numels(1))
    error ('Length of data series is unequal. Correct it.')
end