I have a series of variables form a1 to a100 and each one has a single numeric value. I want to repeat copies of that value to 1-by-4 matrix for all of them.
2 次查看(过去 30 天)
显示 更早的评论
For e.g. a1 = 5; a2 = 6; ......a100 = 43; I want it to be like:- a1 = [5 5 5 5]; a2 = [6 6 6 6]; ... a100 = [43 43 43 43]; I know that, if the variables are too few then it is easily possible by individually using 'repmat'. But how to do it for these many variables?
6 个评论
Stephen23
2018-7-27
I don't see any reason why mixed-integer linear programming requires lots of separate variables: what optimization function are you using?
采纳的回答
Stephen23
2018-7-27
编辑:Stephen23
2018-7-27
Your script Trial1.m (attached) is pointlessly complex, and using eval to access variables names just complicates things even more for no benefit. I doubt that this approach is worthwhile. Most of the first half can be replaced with just one line of code, and the second half is an inefficient use of eval to create lots of variables: keep your data in arrays and just use indexing!
Replace all of this:
load('unitCommData.mat','T','loadProfile');
load('DA_price.mat');
nHours = 24;
nIntervals = nHours*4;
DAPrice = zeros(nIntervals,1);
cnt = 1;
for i = 1:nIntervals
DAPrice(i,1) = e_Auktion24Energie(cnt,1);
if mod(i,4) == 0
cnt = cnt + 1;
end
end
DA_Price = repmat(DAPrice,1,4);
with the much simpler and more robust:
>> ucd = load('unitCommData.mat','T','loadProfile');
>> dap = load('DA_price.mat');
>> P = kron(dap.e_Auktion24Energie,ones(4))
P =
31.72 31.72 31.72 31.72
31.72 31.72 31.72 31.72
31.72 31.72 31.72 31.72
31.72 31.72 31.72 31.72
28.63 28.63 28.63 28.63
28.63 28.63 28.63 28.63
28.63 28.63 28.63 28.63
28.63 28.63 28.63 28.63
27.86 27.86 27.86 27.86
27.86 27.86 27.86 27.86
... more rows here
35.44 35.44 35.44 35.44
35.44 35.44 35.44 35.44
35.44 35.44 35.44 35.44
31.07 31.07 31.07 31.07
31.07 31.07 31.07 31.07
31.07 31.07 31.07 31.07
31.07 31.07 31.07 31.07
28.31 28.31 28.31 28.31
28.31 28.31 28.31 28.31
28.31 28.31 28.31 28.31
28.31 28.31 28.31 28.31
The second half of your code can be simply replaced with this line:
>> X = repmat((1:size(P,1)).',1,4)
X =
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
... more rows here
91 91 91 91
92 92 92 92
93 93 93 93
94 94 94 94
95 95 95 95
96 96 96 96
Which you can trivially access using indexing. So far there is no reason why you require lots of magically named variables.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!