This topic contains 11 replies, has 2 voices, and was last updated by cK 9 years ago.
-
AuthorPosts
-
17 November, 2014 at 10:51 pm #538
Hello,
I am using trying to use for loop to do series of calculations for me. Mathematical expressions for these calculations can be seen in the attached code for the 3 calculation cases mentioned below:
(1) %%%%%%%Prediction at untried sites%%%%%
(2) %Calculation of SSE btw exptal data & predicted values
(3) %%%%%%%%%%%%%%%%%%%calculation of ei at corresponding SSE %%%%%%%%%%%%
Please, how do I go about creating the for loop to make the code better?
Thank you.Attachments:
You must be logged in to view attached files.17 November, 2014 at 11:07 pm #540You can use eval inside a for loop. Its tricky but simple stuff and for your code it should be quite simple. Documentation on eval() has an example too to help in these kind of situations. In this case its something like:
for i = 1:20 expr = sprintf('[Yb%s, var%s] = predictor(%s,dmodel);',… matfiles{i}(2:end), matfiles{i}(2:end), matfiles{i}); eval(expr); end
18 November, 2014 at 12:19 am #541Sorry, I don’t seem to understand the code. ‘[Yb%s, var%s] = predictor(%s,dmodel);’ seems to be a statement. The “%s” does it imply each input data and corresponding output data? If this is so, How does it recognise each of the S…. input data values in a loop? The “matfiles{i}” are they the S… input data? If they are, how are the S…input data recognized?
The problem is how can I pick each of the S… input data values in a loop to do calculation i.e. S_0_07.mat, S_0_085.mat,……etc ?
I don’t really know how to implement the code you gave. Maybe you can help to explain briefly and give examples.
Thanks.18 November, 2014 at 1:15 am #542Sorry I forgot to mention matfiles. I assumed it to be a cell array containing all the mat file names you are using. matfiles{1} could be the string S_0_07.
The examples are given in documentation for eval(). To understand “%s” see formatspec in documentation for sprintf().18 November, 2014 at 5:09 pm #545I’m not sure about this, but I used the code below and got this error message: “Undefined variable “cell_dataS_0_07″ or class “cell_dataS_0_07.mat”. It seems not to be working.
The code is given below:
cell_data={‘S_0_07.mat’,'S_0_085.mat’,'S_0_088.mat’,'S_0_089.mat’,'S_0_0895.mat’,'S_0_091.mat’,'S_0_092.mat’,'S_0_093.mat’,… ‘S_0_094.mat’,'S_0_0945.mat’,'S_0_10.mat’,'S_0_105.mat’,'S_0_11.mat’,'S_0_115.mat’,'S_0_12.mat’,'S_0_125.mat’,…
‘S_0_13.mat’,'S_0_135.mat’,'S_0_15.mat’};
for i = 1:20
expr = sprintf(‘[Yb%s, var%s] = predictor(cell_data%s,dmodel);’,cell_data{i}(2:end), cell_data{i}(2:end), cell_data{i});eval(expr);
endYou can please help to have a look. Thanks.
Attachments:
You must be logged in to view attached files.19 November, 2014 at 11:31 pm #547cell_data%s
creates a string cell_dataS_0_07.mat for i=1. Your files are named S_0_07.mat not cell_dataS_0_07.mat. That explains the error. See the documentation example on eval().24 November, 2014 at 4:24 pm #549Thanks cK. I eventually used:
expr = sprintf(‘[Yb_%s, var_%s] = predictor(%s,dmodel);’,cell_data{i}, cell_data{i}, cell_data{i});
and it worked. Thank you.
Now please permit me to ask an additional question. This code above was just for few values of input S e.g. S_0_07, S_0_085 etc. Now, if I need to use more than 200values of inputs, I think it’s better to write a code which generates the input values rather than loading them.
The input values are each 28 by 3, where the 1st & 2nd columns are always the same while the 3rd column changes. for instance, S=[0,0.01,0.09;1,0.01,0.09;2,0.01,0.09;3,0.01,0.09;4,0.01,0.09;5,0.01,0.09;7,0.01,0.09;0,0.11,0.09;1,0.11,0.09;2,0.11,0.09;3,0.11,0.09;4,0.11,0.09;5,0.11,0.09;7,0.11,0.09;0,0.19,0.09;1,0.19,0.09;2,0.19,0.09;3,0.19,0.09;4,0.19,0.09;5,0.19,0.09;7,0.19,0.09;0,0.34,0.09;1,0.34,0.09;2,0.34,0.09;3,0.34,0.09;4,0.34,0.09;5,0.34,0.09;7,0.34,0.09]; % These are for 3rd column being 0.09.
But I need the same 1st & 2nd columns with different values of the 3rd column which can start from 0.01 to 0.40. So, I’m thinking of having a code which can generate this data for me each time the calculation: [Yb] = predictor(S, dmodel); is required.
Please, how do you think I can achieve this? Thank you.
25 November, 2014 at 5:53 pm #550It’s difficult to see the progressions in the ton of numbers you pasted, but you should be able to use
:
,find()
, logical indexing etc to fill columns. If one or more columns keep same number while other columns have progressing numbers, then such constant column can be created by usingrepmat()
.25 November, 2014 at 9:06 pm #551Thank you for your reply. The data is structured as seen below:
S =
0 0.0100 0.0900
1.0000 0.0100 0.0900
2.0000 0.0100 0.0900
3.0000 0.0100 0.0900
4.0000 0.0100 0.0900
5.0000 0.0100 0.0900
7.0000 0.0100 0.0900
0 0.1100 0.0900
1.0000 0.1100 0.0900
2.0000 0.1100 0.0900
3.0000 0.1100 0.0900
4.0000 0.1100 0.0900
5.0000 0.1100 0.0900
7.0000 0.1100 0.0900
0 0.1900 0.0900
1.0000 0.1900 0.0900
2.0000 0.1900 0.0900
3.0000 0.1900 0.0900
4.0000 0.1900 0.0900
5.0000 0.1900 0.0900
7.0000 0.1900 0.0900
0 0.3400 0.0900
1.0000 0.3400 0.0900
2.0000 0.3400 0.0900
3.0000 0.3400 0.0900
4.0000 0.3400 0.0900
5.0000 0.3400 0.0900
7.0000 0.3400 0.0900This is matrix S for 3rd column being 0.09. The 1st and 2nd column remains the same while 3rd column changes in value within the range of 0.005 to 0.35. So, I need to create matrix for each of these values of the 3rd column (between 0.005 & 0.35), about 127 data. It’ll be cumbersome to load the data one after another. This is the challenge.
You can just give a code which gives a clue on how this can work. Thanks.
26 November, 2014 at 5:04 pm #55527 November, 2014 at 10:16 pm #558Hello,
I eventually wrote this code which seems to have worked but stopped the data generation at the 3rd loop. The error message is:Subscripted assignment dimension mismatch.
Error in data_generation (line 28)
XD(1:7,i)=eval(sprintf(‘XD%d’,i));The function and script required to run “data_generation.m” are found attached.
Kindly help to have a look. Thank you.clear all
clcN =500; k = 1;
Xk = hss(N,k);
r = Xk;
x_hss=r;
X=x_hss;FR=[0.01;0.11;0.19;0.34];
XD=[0;1;2;3;4;5;7];
for i=1:500,
eval(sprintf(‘XD%d=repmat(XD,[1 1])’, i)); %generates 1st columneval(sprintf(‘FR%d=repmat(FR(i,:),[7 1]),’,i)); %generates 2nd column
eval(sprintf(‘Cu%d = repmat(X(i,:),[7 1])’, i)); %generates 3rd column
XD(1:7,i)=eval(sprintf(‘XD%d’,i));
FR(1:7,i)=eval(sprintf(‘FR%d’,i));
Cu(1:7,i)=eval(sprintf(‘Cu%d’,i));
S(1:7,i)=eval(sprintf(‘XD(1:7,i)’,'FR(1:7,i)’,'Cu(1:7,i)’,i)); %generates %500matrices for 1st,2nd &3rd col
endAttachments:
You must be logged in to view attached files.27 November, 2014 at 10:28 pm #561The error means your target on RHS is being assigned a matrix which doesn’t have a compatible size.
repmat(XD,[1 1])
..? That is just XD. Do you mean [7 1] ? -
AuthorPosts
You must be logged in to reply to this topic.