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 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 27 Jul 2018
I don't see any reason why mixed-integer linear programming requires lots of separate variables: what optimization function are you using?
Ankit Singh
Ankit Singh on 27 Jul 2018
Guys thanks for your timely replies...but I found out that the approach I was using was not a viable option, but my objective is now fulfilled as I have changed my approach.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 27 Jul 2018
Edited: Stephen23 on 27 Jul 2018
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.

More Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!