matrix as desired data using for loop
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Stephen23
on 10 Jan 2018
Edited: Stephen23
on 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 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!