matrix as desired data using for loop

1 vue (au cours des 30 derniers jours)
MUKESH KUMAR
MUKESH KUMAR le 10 Jan 2018
Commenté : MUKESH KUMAR le 10 Jan 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

Réponse acceptée

Stephen23
Stephen23 le 10 Jan 2018
Modifié(e) : Stephen23 le 10 Jan 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 commentaires
Stephen23
Stephen23 le 10 Jan 2018
The code I gave you does not care what size A is. Try it.
MUKESH KUMAR
MUKESH KUMAR le 10 Jan 2018
thanks I got it and working well

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements 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