how to do loop on rows matrix?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Risma Fitriyani
am 23 Dez. 2022
Kommentiert: Image Analyst
am 27 Dez. 2022
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
Akzeptierte Antwort
VBBV
am 23 Dez. 2022
Bearbeitet: VBBV
am 23 Dez. 2022
As mentioned in your question, if you have zeros matrix to start with and want to allocate 1s to specfiic zero locations, then you can get the matrix in snapshot using loop as below
A = zeros(3,6); % assume A as your zeros matrix
for k = 1:length(A)-2
if k == 1 | k == 2 | k == 3
A(k,k) = 1;
elseif k == 4
A(1,k) = 1;
A(2,k+1) = 1;
A(3,k+2) = 1;
end
end
A
2 Kommentare
VBBV
am 23 Dez. 2022
It depends on how you want final matrix to look like. That is "in which specific positions you want to allocate 1s in zero matrix"
Weitere Antworten (2)
Image Analyst
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
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.
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!