Initializing all rows with one set of values in one half and another set in the other half
1 次查看(过去 30 天)
显示 更早的评论
The following line of code works for initializing the first row of an array:
cA(1,:)=[cA0*ones(1,(N-1)/2+1),Kw/cB0*ones(1,(N+1)/2-1)];
However, I would like to initialize all rows of the array with this "code". I thought the following would work, but it doesn't.
cA(:,:)=[cA0*ones(1,(N-1)/2+1),Kw/cB0*ones(1,(N+1)/2-1)];
MatLab is throwing up an error that it doesn't recognize function or variable 'ones'. How do I initialize all rows in cA with what I originally had for just the first row.
0 个评论
采纳的回答
Walter Roberson
2021-9-20
cA(:,:) = repmat([cA0*ones(1,(N-1)/2+1),Kw/cB0*ones(1,(N+1)/2-1)], size(cA,1), 1);
Or consider
nrow = size(cA,1);
cA(:,:) = [cA0*ones(nrow,(N-1)/2+1),Kw/cB0*ones(nrow,(N+1)/2-1)];
Unless cA has some special attributes you want to save, such as you wanting to preserve single(), then you would normally use
nrow = size(cA,1);
cA = [cA0*ones(nrow,(N-1)/2+1),Kw/cB0*ones(nrow,(N+1)/2-1)];
And more generally, you would typically use
mid = ceil(N/2);
cA(:, 1:mid) = cA0;
cA(:, mid+1:N) = Kw/cB0;
0 个评论
更多回答(1 个)
David Hill
2021-9-20
cA=repmat([cA0*ones(1,(N-1)/2+1),Kw/cB0*ones(1,(N+1)/2-1)],number_of_rows,1);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Object Containers 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!