필터 지우기
필터 지우기

how to compare one element in an array with all the other elements in another array? And repeating it for n elements in the first array?

조회 수: 39 (최근 30일)
A = [ 1, 2,3,4,5]
B = [ 3,4,2,6,8,0]
Now , take 1st element (i.e = 1) in vector A and compare it with all elements in vector B. And after comparing it with all elements in B and now we need to consider the second element of A ( i.e 2) and do the same. Finish the loop once we iterate with all the elements in the first vector.
  댓글 수: 6
Geoff Hayes
Geoff Hayes 2018년 4월 6일
Aswin - I think that you need to show what you expect with a small example.
Aswin Sandirakumaran
Aswin Sandirakumaran 2018년 4월 7일
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]

댓글을 달려면 로그인하십시오.

답변 (1개)

dpb
dpb 2018년 4월 6일
A = [ 1, 2,3,4,5]
B = [ 3,4,2,6,8,0]
Taking a guess, and what was written literally,
>> for i=1:length(A)
B(A(i)<B)=B(A(i)<B)-A(i);
end
>> B
B =
2 1 1 3 2 0
>>
Above is repetitive and sequential so the test is repeated on B after the prior elements have already done their duty; hence the second iteration and subsequent operate on an already-modified B.
Above could save a comparison if used a temporary logical vector as
for i=1:length(A)
isLess=(A(i)<B);
B(isLess)=B(isLess)-A(i);
end
which may also be a little easier to read...
  댓글 수: 2
Aswin Sandirakumaran
Aswin Sandirakumaran 2018년 4월 7일
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]
dpb
dpb 2018년 4월 8일
Don't try to create "poof" new variables into the workplace programmatically; use cell arrays to hold the variably-sized results of each step.
Looks to me as though the above example fails in the second step, however, as there are only two elements left in A after the first step so which is <length(B) so length(D) == 2???

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by