matrix as desired data using for loop

1 visualización (últimos 30 días)
MUKESH KUMAR
MUKESH KUMAR el 10 de En. de 2018
Comentada: MUKESH KUMAR el 10 de En. de 2018
suppose I had following matrix A
A(1,:)=[10 10 0.2 0 0]; A(2,:)=[10 10 10 0.3 0]; A(3,:)=[10 0.5 0 0 0];
Now I want a cell array B like this
B= [{5*5};{5*5};{5*5}];
like
B(1)=[10 10 0.2 0 0;0 10 10 0.2 0;0 0 10 10 0.2;0.2 0 0 10 10;10 0.2 0 0 10]
similarly create
B(2)=[[10 10 10 0.3 0;0 10 10 10 0.3;03 0 10 10 10;10 0.3 0 10 10;10 10 0.3 0 10]
and B(3) also.
thanks

Respuesta aceptada

Stephen23
Stephen23 el 10 de En. de 2018
Editada: Stephen23 el 10 de En. de 2018
In just two lines (could easily be written in one line):
>> A = [10,10,0.2,0,0;10,10,10,0.3,0;10,0.5,0,0,0];
>> fun = @(v)toeplitz(v(1,[1,end:-1:2]),v(1,:));
>> B = cellfun(fun,num2cell(A,2),'uni',0);
>> B{:}
ans =
10.00000 10.00000 0.20000 0.00000 0.00000
0.00000 10.00000 10.00000 0.20000 0.00000
0.00000 0.00000 10.00000 10.00000 0.20000
0.20000 0.00000 0.00000 10.00000 10.00000
10.00000 0.20000 0.00000 0.00000 10.00000
ans =
10.00000 10.00000 10.00000 0.30000 0.00000
0.00000 10.00000 10.00000 10.00000 0.30000
0.30000 0.00000 10.00000 10.00000 10.00000
10.00000 0.30000 0.00000 10.00000 10.00000
10.00000 10.00000 0.30000 0.00000 10.00000
ans =
10.00000 0.50000 0.00000 0.00000 0.00000
0.00000 10.00000 0.50000 0.00000 0.00000
0.00000 0.00000 10.00000 0.50000 0.00000
0.00000 0.00000 0.00000 10.00000 0.50000
0.50000 0.00000 0.00000 0.00000 10.00000
  3 comentarios
Stephen23
Stephen23 el 10 de En. de 2018
The code I gave you does not care what size A is. Try it.
MUKESH KUMAR
MUKESH KUMAR el 10 de En. de 2018
thanks I got it and working well

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by