matrix as desired data using for loop

1 次查看(过去 30 天)
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

采纳的回答

Stephen23
Stephen23 2018-1-10
编辑:Stephen23 2018-1-10
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 个评论
Stephen23
Stephen23 2018-1-10
The code I gave you does not care what size A is. Try it.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by