Forcing matrices to match in size

Let's say I have two single-row matrices A and B.
I want to check if A and B have the same number of columns. If they do, do nothing.
If not, I want to remove the last character in B.
For example, if we have
A = [30 25 78]
B = [17 91 44 83]
Then I want to remove the last column of B, leaving
A = [30 25 78]
B = [17 91 44]
But if we originally had
A = [30 25 78]
B = [17 91 44]
then do nothing. What scheme could accomplish this?

답변 (2개)

Arturo Mendoza Quispe
Arturo Mendoza Quispe 2019년 9월 8일

0 개 추천

B = B(1:numel(A));
This will set B to have the size of A, hence there is no need for an if statement.

댓글 수: 3

Not correct according to the problem statement. If A is longer than B, then the number of columns do not match and it is required to remove the last column in B, even though that would make B even shorter than A. Also, if A were (say) 3 shorter than B, then according to the problem statement, only the last column of B is to be removed, not the last two columns.
The logic is:
is length(A) the same as length(B)
if not then remove the last column of B
It is not clear what is intended to happen if B is already empty but A is not empty.
g
g 2019년 9월 8일
편집: g 2019년 9월 8일
Based on what I'm working with, the number of columns in B is always greater than or equal to the number of columns in A. I should have been more precise.
Walter Roberson
Walter Roberson 2019년 9월 8일
But you still only want to remove one column of B in that case, right? Because your problem title talks about making them match. Arturo's solution is great to make them match, but is not the correct solution if exactly one column is to be removed from B.

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

Walter Roberson
Walter Roberson 2019년 9월 8일

0 개 추천

B = B(1:end-(length(A) ~= length(B)));
Leaves B the same length if A and B are the same length, and otherwise removes exactly 1 element from B.

카테고리

도움말 센터File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

질문:

g
g
2019년 9월 7일

답변:

2019년 9월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by