how to do loop on rows matrix?
Ältere Kommentare anzeigen
i have zeros matrix which has 3 rows and 6 columns. i want to allocate “1” on zeros matrix for each columns in order to get the result output like this attached picture. can anyone be kind to help me? thanks in advance
1 Kommentar
Risma Fitriyani
am 23 Dez. 2022
Akzeptierte Antwort
Weitere Antworten (2)
It looks like the 1's are only needed on the diagonal elements.
You can do this with:
repmat(eye(3),1,2)
4 Kommentare
Risma Fitriyani
am 26 Dez. 2022
A more general approach would be:
m = 15;
n = 25;
A = repmat(eye(m),1,ceil(n/m)) %repeat I and store in A.
A = A(:,1:n) %cut off last columns from Matrix A
m and n define the size of your result.
Note that the repmat command results in a matrix that generally would be too wide (
here ). Therefore you have to cut off the last 5 columns (or take the first n columns of matrix
).
here ). Therefore you have to cut off the last 5 columns (or take the first n columns of matrix
Risma Fitriyani
am 26 Dez. 2022
Risma Fitriyani
am 26 Dez. 2022
% For 3x6:
c = [1 0 0 1 0 0];
r = [1 0 0];
output = toeplitz(r, c)
% For 15 by 25
c = [1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1];
r = [1; zeros(15, 1)];
output = toeplitz(r, c)
3 Kommentare
Markus M.
am 26 Dez. 2022
Didn't think about this interpretation. @Risma Fitriyani can you describe in more detail, what you want your matrix to look like?
Risma Fitriyani
am 26 Dez. 2022
Image Analyst
am 27 Dez. 2022
@Risma Fitriyani did you not like my answer for the 15x25? Does the answer you accepted work?
Again, can you give the full output for your 15x25 matrix like @Markus M. directly asked you for? I'd like to see how my solution does not do what you want.
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
