How to add, multiply and subtract two matrices have different length ?

If I have matrix A =[2 2 1; 1 2 5; 1 2 3], B=[1 2; 1 1],
How can I add or subtract these matrices while they have different length ?

1 commentaire

Mathematical, there are no such operations for matrices of different sizes.
So you first will have to explain us how you intend to define these "additions and subtractions".

Connectez-vous pour commenter.

 Réponse acceptée

While adding two matrices of different dimensions is not defined mathematically, it is possible to add or subtract them by indexing into them, however it is first necessary to define what elements are to be affected in the larger matrix.
Here is one approach —
A = [2 2 1; 1 2 5; 1 2 3]
A = 3×3
2 2 1 1 2 5 1 2 3
B = [1 2; 1 1]
B = 2×2
1 2 1 1
idx = 1:numel(B); % Using The Most Obvious Index Vector ...
C = A;
C(idx) = A(idx) + B(idx)
C = 3×3
3 3 1 2 2 5 3 2 3
idx = randperm(numel(A), numel(B)) % Using A Different, Random, Index Vector ...
idx = 1×4
4 6 5 1
C = A;
C(idx) = A(idx) + B(1:numel(B))
C = 3×3
3 3 1 1 4 5 1 3 3
So in a limited sense it is possible, however I have no way of knowing if either of these produces the desired result.
.

8 commentaires

omar th
omar th le 13 Mar 2022
Modifié(e) : omar th le 13 Mar 2022
I will try this way because I tried to add dummy zeros but didn't work because each itration the dimentions will be changed due to matrices produced from assigned number of points to number of another points that's why each itration is changed for example the first one matrix A=(20,2) while in the second one maybe become matrix A =(50,25) therefore when I add dummy zeros doesn't work.
matrices produced from or related to random distribution
Thank you!
The indexing approach will work.
You need to decide if it works correctly and if it produces the result you want.
Thanks in advance, can I apply index approach in multiplication matrices ?
Are you sure you will be able to interprete results obtained from such artificial arithmetic operations ?
@omar th — Yes, with element-wise (array) operations, multiplication and division as well.
@Star Strider Unfortunately index approach doesn't produce the desired results
That depends on what the desired results are.
A = randi(9,3)
A = 3×3
1 2 1 3 7 3 6 2 4
B = randi(9,2)
B = 2×2
9 4 6 9
C = A;
C(1:2,1:2) = A(1:2,1:2) + B
C = 3×3
10 6 1 9 16 3 6 2 4
.
@omar th you forgot to read Community Guidelines or these posting guidelines:
So, because of that you forgot to state what the desired results are! I guess you also missed Star's gentle hint. So, what are they? What elements get subtracted from what elements, and what elements are ignored during the operation?

Connectez-vous pour commenter.

Plus de réponses (1)

Perhaps this is what you want -- to subtract the upper left parts that overlap.
A = [2 2 1; 1 2 5; 1 2 3]
B = [1 2; 1 1]
[rowsA, colsA] = size(A)
[rowsB, colsB] = size(B)
maxRow = max([rowsA, rowsB])
maxCol = max([colsA, colsB])
% Pad dimensions if nexessary
if rowsA > rowsB
B(maxRow, end) = 0;
[rowsB, colsB] = size(B) % Update size
elseif rowsB > rowsA
A(maxRow, end) = 0;
[rowsA, colsA] = size(A) % Update size
end
if colsA > colsB
B(end, maxCol) = 0;
[rowsB, colsB] = size(B) % Update size
elseif colsB > colsA
A(end, maxCol) = 0;
[rowsA, colsA] = size(A) % Update size
end
% Let's see what they are now:
A
B
% Now do the subtraction of the upper left overlapping parts.
C = A - B
You get:
A =
2 2 1
1 2 5
1 2 3
B =
1 2 0
1 1 0
0 0 0
C =
1 0 1
0 1 5
1 2 3

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by