expanding an array

4 ビュー (過去 30 日間)
Anand Anand
Anand Anand 2011 年 4 月 3 日
I need to compare 2 arrays A and B which are unequal in length.So,I need to find the shorter of the 2 arrays and add additional zeros in it so that the arrays are of the same length and so they can be compared by 'if' statement

回答 (2 件)

the cyclist
the cyclist 2011 年 4 月 3 日
Another way, that truncates rather than appends zeros. (I know you specified otherwise, but thought this might be useful as well.)
A = 1:10
B = 1:8
shorterLength = min(length(A),length(B));
compare = A(1:shorterLength)==B(1:shorterLength)

Walter Roberson
Walter Roberson 2011 年 4 月 3 日
What is the point? The two arrays cannot be the same if they have a different number of elements.
tA = A;
tB = B;
LD = length(A) - length(B);
if LD < 0
tA(end-LD) = 0; %extends matrix!
elseif LD > 0
tB(end+LD) = 0; %extends matrix!
end
if isequal(tA, TB)
...
end

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by