how to do loop on rows matrix?

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

采纳的回答

VBBV
VBBV 2022-12-23
编辑:VBBV 2022-12-23
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
A = 3×6
1 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 1
  2 个评论
Risma Fitriyani
Risma Fitriyani 2022-12-23
Thank you for your help, it means a lot to me. Then, what if I have a matrix with a large number of rows and columns like 25x100, should I write it to A(25,k+2)? Or maybe there is a more effective way?
VBBV
VBBV 2022-12-23
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"

请先登录,再进行评论。

更多回答(2 个)

Markus M.
Markus M. 2022-12-23
编辑:Markus M. 2022-12-23
It looks like the 1's are only needed on the diagonal elements.
You can do this with:
repmat(eye(3),1,2)
  4 个评论
Risma Fitriyani
Risma Fitriyani 2022-12-26
this is worked! thank you so much, this helped me a lot.

请先登录,再进行评论。


Image Analyst
Image Analyst 2022-12-26
Instead of a for loop, you can use the toeplitz function:
% For 3x6:
c = [1 0 0 1 0 0];
r = [1 0 0];
output = toeplitz(r, c)
output = 3×6
1 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 1
% 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)
output = 16×25
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 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1
  3 个评论
Risma Fitriyani
Risma Fitriyani 2022-12-26
i want the final output would be like the attached picture above (on my question), but i have different various size matrix, that’s why i asked you how if i have size matrix 15x25 because matrix with 3x6 is only example to explain my needs
Image Analyst
Image Analyst 2022-12-27
@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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by