generate matrix by for loop
2 views (last 30 days)
Show older comments
A=[1 -10 4
0 -2 7
5 1 9]
I need to get
A=[1 0 -10 0 4
0 0 -2 0 7
5 0 1 0 9]
by for loop
1 Comment
David Meissner
on 6 Jun 2022
I would look into "Indexing with a Single Index" here: https://www.mathworks.com/help/matlab/math/array-indexing.html
Accepted Answer
DGM
on 6 Jun 2022
Don't need a loop.
A = [1 -10 4
0 -2 7
5 1 9];
B = zeros(size(A,1),2*size(A,2)-1); % allocate
B(:,1:2:end) = A % fill in nonzero columns
5 Comments
DGM
on 6 Jun 2022
Either answer will zero-pad columns regardless of the size or contents of A.
If you're trying to describe a particular generalized relationship between input and output, you'll have to clarify with both inputs and outputs for your example.
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!