How to compare two arrays to find in which column a value from one array is greater than value of other array

3 Ansichten (letzte 30 Tage)
I want to compare values out of same column of different arrays (of different size) and then to kwow at which column a value of one array is greater than value of other array. The first comparisson are between the values of the first column. The second comparisson are between te values of the second column, and so forth.
Voor instance:
A = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
B = [1, 2, 3, 5, ,8, 13, 21]
At which column is B > 2 * A.
Here the answer will be 6 (because in the 6th column is 13 > 2 * 6 and thus is the first column from left to right were B > 2*A)

Akzeptierte Antwort

Dyuman Joshi
Dyuman Joshi am 21 Okt. 2023
A = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
B = [1, 2, 3, 5, 8, 13, 21];
n = min(numel(A),numel(B));
%Comparison
com = B(1:n)>2*A(1:n);
%Find the index first occurence where comparison is true
idx = find(com, 1)
idx = 6

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by