# how to multiply every single element in a matrix to entire of another matrix?

4 vues (au cours des 30 derniers jours)
majid le 7 Avr 2024
Modifié(e) : majid le 7 Avr 2024
Hello.
I want to multiply every single element in a matrix to entire of another matrix.
x = [100,500,900,1300,1700;
120,600,1080,1560,2040;
140,700,1260,1820,2380;
160,800,1440,2080,2720;
180,900,1620,2340,3060];
z = rand (4,20);
I want to multiply 100 to entire z and then 500 and so on... but when each multipication occured saved it in new indexed parameter.
how can I do this?
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Paul le 7 Avr 2024
Here's one option to store each result in a cell array that's the same size as x
x = [100,500,900,1300,1700;
120,600,1080,1560,2040;
140,700,1260,1820,2380;
160,800,1440,2080,2720;
180,900,1620,2340,3060];
%z = rand (4,20);
z = [1 2;3 4]; % small z for example
S = cellfun(@(x) x*z,mat2cell(x,ones(1,size(x,1)),ones(1,size(x,2))),'Uni',false)
S = 5x5 cell array
{2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double}
% check
S{1,1} - x(1,1)*z
ans = 2x2
0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
S{3,3} - x(3,3)*z
ans = 2x2
0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
##### 6 commentairesAfficher 4 commentaires plus anciensMasquer 4 commentaires plus anciens
Bruno Luong le 7 Avr 2024
Modifié(e) : Bruno Luong le 7 Avr 2024
Shorter
x = [100,500,900,1300,1700;
120,600,1080,1560,2040;
140,700,1260,1820,2380;
160,800,1440,2080,2720;
180,900,1620,2340,3060];
%z = rand (4,20);
z = [1 2;3 4]; % small z for example
S = arrayfun(@(xij) xij*z,x,'Uni',false)
S = 5x5 cell array
{2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double}
majid le 7 Avr 2024
thank you for guiding me.

Connectez-vous pour commenter.

### Plus de réponses (2)

Steven Lord le 7 Avr 2024
If you're asking can you dynamically create variables with numbered names like x1, x2, x3, etc.? Yes.
Should you do this? The general consensus is no. That Discussions post explains why this is generally discouraged and offers several alternative approaches.
One such approach is to use pages of a 3-dimensional array.
x = [100,500,900,1300,1700;
120,600,1080,1560,2040;
140,700,1260,1820,2380;
160,800,1440,2080,2720;
180,900,1620,2340,3060];
z = rand (4,20);
zv = reshape(z, 1, 1, []);
result = x.*zv;
To check:
isequal(result(:, :, 42), x.*z(42))
ans = logical
1
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
majid le 7 Avr 2024
Modifié(e) : majid le 7 Avr 2024

Connectez-vous pour commenter.

Bruno Luong le 7 Avr 2024
Modifié(e) : Bruno Luong le 7 Avr 2024
x = [100,500,900,1300,1700;
120,600,1080,1560,2040;
140,700,1260,1820,2380;
160,800,1440,2080,2720;
180,900,1620,2340,3060];
%z = rand (4,20);
z = [1 2;3 4]; % small z for example
[mx,nx] = size(x);
[mz,nz] = size(z);
C = mat2cell(kron(x,z),repelem(mz,mx),repelem(nz,nx))
C = 5x5 cell array
{2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double} {2x2 double}
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
majid le 7 Avr 2024
Modifié(e) : majid le 7 Avr 2024
thankyou for answering .

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Operating on Diagonal Matrices 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