set multiple columns of matrix simultaneously

Hello,
I want to set multiple columns of a matrix to the same column vector. What I am currently doing is itterating over each column and changing them individually.
DM_sine = zeros(32);
for i=1:length(DM_sine);
DM_sine(:,i) = sin(2*pi*i/32);
end
This does what I want, but seems massively inefficient and inelegant. Is there a better alternative?

 채택된 답변

Star Strider
Star Strider 2016년 7월 6일

1 개 추천

Vectorise the first row, and then use repmat to duplicate it down the columns:
DM_sine = zeros(32);
i=1:length(DM_sine);
DM_sine = repmat(sin(2*pi*i/32), size(DM_sine,1), 1);
sample = DM_sine(1:5, 1:5) % View Sample (Not Required)
sample =
0.19509 0.38268 0.55557 0.70711 0.83147
0.19509 0.38268 0.55557 0.70711 0.83147
0.19509 0.38268 0.55557 0.70711 0.83147
0.19509 0.38268 0.55557 0.70711 0.83147
0.19509 0.38268 0.55557 0.70711 0.83147

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

태그

질문:

2016년 7월 5일

답변:

2016년 7월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by