Comparing Matrices of Differing Dimensions, and Creating a New Matrix which meets a criteria

조회 수: 1 (최근 30일)
I have two row vectors: A = [1 2 2 3 4 5 6], B = [2 1 45 6]
I want all the elements of B that are bigger than A to be eliminated and a new matrix with the remaining elements to be saved.
This is what I have so far:
A(A<=B) = []
But this only works if the matrix dimensions are the same, is there any way to do this comparison and return a matrix which satisfy the condition?
Thanks, Chris
  댓글 수: 4
KSSV
KSSV 2020년 7월 25일
편집: KSSV 2020년 7월 25일
You can make dimensions equal by adding NaN's to smaller array.
A = [1 2 2 3 4 5 6] ;
B = [2 1 45 6] ;
C = NaN(size(A)) ;
C(1:length(B)) = B ;
A(A<=C) =[]
Chris Stout
Chris Stout 2020년 7월 25일
Thank you I will keep this in mind, as there is definitely multiple scenarios I can see where I will need to do this.

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

채택된 답변

madhan ravi
madhan ravi 2020년 7월 25일
B(all(A(:) < B)) = []

추가 답변 (0개)

카테고리

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