필터 지우기
필터 지우기

Problem with Max function in a if loop

조회 수: 1 (최근 30일)
Simon
Simon 2013년 5월 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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by