Problem with Max function in a if loop

2 次查看(过去 30 天)
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 个评论
Simon
Simon 2013-5-30
编辑:Simon 2013-5-30
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 2013-5-30
Please show size(BLOG) class(BLOG{2,1}) size(BLOG{2,1})

请先登录,再进行评论。

采纳的回答

Hadi Hajieghrary
Hadi Hajieghrary 2013-5-30
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 个评论
Walter Roberson
Walter Roberson 2013-5-30
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.

请先登录,再进行评论。

更多回答(2 个)

Simon
Simon 2013-5-30
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 个评论
Walter Roberson
Walter Roberson 2013-5-30
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 2013-5-31
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...

请先登录,再进行评论。


Simon
Simon 2013-5-31
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 个评论
Jan
Jan 2013-5-31
@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.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by