Borrar filtros
Borrar filtros

How to Compare two arrays and do something if it is less ? The algorithm should work as below mentioned

1 visualización (últimos 30 días)
A=[1,2,3,4,5] B=[6,7,8] then the code should take the 1st element in A (ie. 1) and compare it with B, if ( the first element is smaller (A<B) do B - A and store that value in a new array. Then go for the 2nd Element in A (ie. 2) and compare it with 2nd element in array B ( 2< 7) therefore do B - A and store it in C[2]. and so on
A = [1,2,3,4,5] B = [6,7,8] % C should be the new result after comparing A[i] < B[i] C[i] = B[i] - A[i]; C = [5,5,5] Once done we are left with 4th,5th element in array A. if length of array A exceeds length of array B. Then the elements after length of A = length of array B should comparing from the index 1 of array C. But now it should compare with the new C element A = [4,5] % new A C = [5,5,5] % new C repeat the same process as above while comparing and get new array Final Result: D = [1,0,5]

Respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by