Frage

I have multiple array of real numbers. Eg.

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

I want a shorter code, if number of arrays are in 100's. What i know is,

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

This will display an error if length if all series is not equal but how could it be done in small code when array number and size is very large (in 100's) ? Thank you.

War es hilfreich?

Lösung

Code

%%// 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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top