matrix as desired data using for loop

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Stephen23
Stephen23 il 10 Gen 2018
Modificato: Stephen23 il 10 Gen 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 Commenti
Stephen23
Stephen23 il 10 Gen 2018
The code I gave you does not care what size A is. Try it.
MUKESH KUMAR
MUKESH KUMAR il 10 Gen 2018
thanks I got it and working well

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by