Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
deleted 9 characters in body
Source Link
Leonid Beschastny
  • 51.6k
  • 10
  • 121
  • 126

The simplest way I can think of is to combine arrayfun and cell2mat functions:

B = cell2mat(arrayfun((@(x) T .* x), A, 'UniformOutput', false)); 

First, we transform ourI transformed matrix A into a cell array of matrices T .* x, where x is an element of A (a assumed here that T is a matrix).

Then we useI used cell2mat to transform in back into a matrix.

Here is a complete example (execute online):

A = magic(3); T = diag([-1 1]); B = cell2mat(arrayfun((@(x) T .* x), A, 'UniformOutput', false)); 

resulting in:

B = -8 0 -1 0 -6 0 0 8 0 1 0 6 -3 0 -5 0 -7 0 0 3 0 5 0 7 -4 0 -9 0 -2 0 0 4 0 9 0 2 

The simplest way I can think of is to combine arrayfun and cell2mat functions:

B = cell2mat(arrayfun((@(x) T .* x), A, 'UniformOutput', false)); 

First, we transform our matrix A into a cell array of matrices T .* x, where x is an element of A (a assumed here that T is a matrix).

Then we use cell2mat to transform in back into a matrix.

Here is a complete example:

A = magic(3); T = diag([-1 1]); B = cell2mat(arrayfun((@(x) T .* x), A, 'UniformOutput', false)); 

resulting in:

B = -8 0 -1 0 -6 0 0 8 0 1 0 6 -3 0 -5 0 -7 0 0 3 0 5 0 7 -4 0 -9 0 -2 0 0 4 0 9 0 2 

The simplest way I can think of is to combine arrayfun and cell2mat functions:

B = cell2mat(arrayfun((@(x) T .* x), A, 'UniformOutput', false)); 

First, I transformed matrix A into a cell array of matrices T .* x where x is an element of A (a assumed that T is a matrix).

Then I used cell2mat to transform in back into a matrix.

Here is a complete example (execute online):

A = magic(3); T = diag([-1 1]); B = cell2mat(arrayfun((@(x) T .* x), A, 'UniformOutput', false)); 

resulting in:

B = -8 0 -1 0 -6 0 0 8 0 1 0 6 -3 0 -5 0 -7 0 0 3 0 5 0 7 -4 0 -9 0 -2 0 0 4 0 9 0 2 
Source Link
Leonid Beschastny
  • 51.6k
  • 10
  • 121
  • 126

The simplest way I can think of is to combine arrayfun and cell2mat functions:

B = cell2mat(arrayfun((@(x) T .* x), A, 'UniformOutput', false)); 

First, we transform our matrix A into a cell array of matrices T .* x, where x is an element of A (a assumed here that T is a matrix).

Then we use cell2mat to transform in back into a matrix.

Here is a complete example:

A = magic(3); T = diag([-1 1]); B = cell2mat(arrayfun((@(x) T .* x), A, 'UniformOutput', false)); 

resulting in:

B = -8 0 -1 0 -6 0 0 8 0 1 0 6 -3 0 -5 0 -7 0 0 3 0 5 0 7 -4 0 -9 0 -2 0 0 4 0 9 0 2