Problem with Max function in a if loop

2 views (last 30 days)
So, basically my max function at the end gives me a random number and does not apply my if and else if constraint :/
for i = 1:B
JIM{i} = find(REG{1,i} > 0.5);
end
Ans = [JIM;REG;COEFF];%Merge REG and Coeff together so that we can keep an eye on variables name!
for i = 1:B
for j = 1:C
D{i} = find(Ans{3,i}.tStat(:).^2 > 3.8416);
end
end
BLOG = [Ans;D];
for i = 1:B
SIZE{i} = size(BLOG{4,i});
end
for i = 1:B
if BLOG{1,i} ==1
elseif SIZE{1,i}(1,1) ==test.NumCoefficients
H = max(BLOG{2,:});
end
end
end
  4 Comments
Simon
Simon on 30 May 2013
Edited: Simon on 30 May 2013
I wanted the maximum value of the row and didn't know apparently how to set the if statements :/
I'm a noob and i'm just trying to survive my intership
Walter Roberson
Walter Roberson on 30 May 2013
Please show size(BLOG) class(BLOG{2,1}) size(BLOG{2,1})

Sign in to comment.

Accepted Answer

Hadi Hajieghrary
Hadi Hajieghrary on 30 May 2013
As I know, the max function is not going to work on a cell data structure. Try to use a matrix. In your case just simply change '{}' to '()'.
  1 Comment
Walter Roberson
Walter Roberson on 30 May 2013
The code has
max(BLOG{2,:})
which uses {} not (). Therefor it is not operating on a cell data structure, not unless BLOG{2,:} stored cell data structures. But BLOG(2,:) appears to be REG and the code has REG{1,i} in the context of a numeric array so BLOG{2,:} should be a list of numeric arrays.
Now, {} expansion of an array is like writing the elements out one by one, so
max(BLOG{2,:})
is like writing
max(BLOG{2,1}, BLOG{2,2}, BLOG{2,3}...)
and so on for however many entries are in BLOG. I'm not sure but I think it might be (B) entries.
max() applied to multiple matrices finds the maximum of each corresponding position, rather than finding the maximum over all of the values. I the maximum over all of the values is desired then the code would have to be
max([BLOG{2,:}])
The code is confusing to read; it seems quite likely to me that it could be written more clearly.

Sign in to comment.

More Answers (2)

Simon
Simon on 30 May 2013
Ok thx guys. I will try to correct my code tommorrow at the office. Thank you very much. If any of you knows some trick to write more clearly pleasy feel free to educate me :) i know i need to learn alot on matlab!
  2 Comments
Walter Roberson
Walter Roberson on 30 May 2013
I can't tell what the algorithm is intended to be.
I can say, though, that it seems confusing to package things up into Ans and BLOG and then pull out specific elements of those that appear to be intended to correspond to the parts that were packaged together. Instead of BLOG{2,:} why not refer to D{:} ?
Simon
Simon on 31 May 2013
I thought it would be good to make a matrix with 2 rows of constraints (1,[]) so that after i could just pick the the answer with the highest rsquared that respected the two constraint...

Sign in to comment.


Simon
Simon on 31 May 2013
In my 'answer' matrix, row (1,:)is all [] or 1 whether the rsquared is higher than 0,5 or not. The second row (2,:) is the Rsquared of all 15 regressions. The third row (3,:) is the mdl.Coefficients of all 15 regression (including tstats) and the fourth row (4,:) is D{i} = find(Ans{3,i}.tStat(:).^2 > 3.8416, representing the number of coefficients that have a tStat over 1.96 or less than -1.96. I did the .^2 to make it simple (1.96^2 = 3.8416). My plan was to find a way to ONLY get one result :the highest Rsquared IF Rsquared > 0,5 and IF all coeff have tStats^2 over 3.8416.... That sound like incredibly hard to me XD
  1 Comment
Jan
Jan on 31 May 2013
@Simon: Please post only answers in the answer section. Larger threads can be understood easier, if the standard structure is applied using the question, comments and answers specifically. Thanks.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!