Forcing matrices to match in size
21 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
回答 (2 件)
Arturo Mendoza Quispe
2019 年 9 月 8 日
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 件のコメント
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
2019 年 9 月 8 日
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Multidimensional Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!