Error using element-wise multiplication

2 vues (au cours des 30 derniers jours)
James Mitchell
James Mitchell le 9 Juin 2017
Commenté : James Mitchell le 9 Juin 2017
This could be a simple thing and I'm just overlooking it. A little context, I am swapping out the first row with the last row in a non-square matrix, and then multiplying the new matrix with the original. I'm struggling to find a way to multiply the two matrices. I've tried with square matrices and it works perfectly so I'm at a bit of a loss for some reason.
A=[7 3 10 10 -2 3
9 -8 10 0 9 -10
-8 -5 -7 6 6 7
9 1 10 -8 10 9];
A([1 end],:) = A(end 1],:); % To swap the first and the last rows
B = A([end 1],:) .* A % Multiply the new swapped matrix with the original
Error using .*
Matrix dimensions must agree.

Réponse acceptée

James Tursa
James Tursa le 9 Juin 2017
Modifié(e) : James Tursa le 9 Juin 2017
Remember, the syntax A([end 1],:) is only 2 rows, not the entire matrix. That is why you are getting the error. To do what you want to do, simply save a copy of the original.
Acopy = A;
A([1 end],:) = A([end 1],:); % To swap the first and the last rows
B = Acopy .* A; % Multiply the new swapped matrix with the original
  1 commentaire
James Mitchell
James Mitchell le 9 Juin 2017
Awesome! Thank you, almost as soon as I posted the question I realized that I was creating a matrix of only two rows.. Although I still couldn't think of saving the original under a different variable. Thanks again!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by