Storing Maximum values from each row

9 views (last 30 days)
Cat
Cat on 28 Oct 2014
Commented: Adam on 28 Oct 2014
Hi I am trying to find the minimum peak force and so I am trying to find the maximum value in each row, store that and then later choose the minimum from the matrix of maximums. I would then also need the corresponding variable values which give this minimum force. As you can see in my code, I am trying to do this using (i,:) to look at a specific row and all the possible 'j' (column) values. However, I think that it is only storing the maximum value from the last iteration/row rather than from every iteration/row. For the problem we were instructed to store the maximum value if it is larger than the last, hence the if F(i,:)>F_max bit. Any help much appreciated! Thank you in advance. This is my code...
m=4000; %kg
g=9.81;
L=4; %m
r_min=1.2; %m
r_max=2.2; %m
r=linspace(1.2,2.2,37); %m
theta_min=-20; %degrees
theta_max=80; %degrees
theta=linspace(-20,80,37); %degrees
phi=linspace(0,180,37); %degrees
gamma=phi+20; %degrees
%-----------------------------------
F_max=0;
for i=1:37
c_2(i)=((r_max^2)-(r_min^2))./(cosd(gamma(i)+theta_min)-cosd(gamma(i)+theta_max));
c_1(i)=(r_max^2)+c_2(i).*cosd(gamma(i)+theta_max);
a(i)=real(0.5*(((c_1(i)+c_2(i)).^(1/2))+((c_1(i)-c_2(i)).^(1/2))));
b(i)=real(c_2(i)/(2*a(i)));
for j=1:37
r(i,j)=sqrt((a(i)^2)+(b(i)^2)-(2*a(i)*b(i)*cosd(gamma(i)+theta(j))));
F(i,j)=(r(i,j)*m*g*L*cosd(theta(j)))/(b(i)*a(i)*sind(gamma(i)+theta(j)));
end
k=1;
if F(i,:)>F_max & F(i,:)~=Inf & F(i,:)~=-Inf
F_new(k)=F(i,:);
k=1+k;
end
F_max=max(k);
F_min=min(k);
theta_opt=theta(:);
a_opt=a(:);
b_opt=b(:);
r_opt=r(:);
phi_opt=gamma(:)-20;
end
Currently when run this code is giving the error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in MatLab_Code_main_3 (line 56)
F_new(k)=F(i,:);

Accepted Answer

Adam
Adam on 28 Oct 2014
Edited: Adam on 28 Oct 2014
At a glance...
F_max(k) = F_new;
looks to be what you need, but with simply
F_new = F(i,:);
inside your if statement. You will get warnings about an array growing inside a loop, but that doesn't really matter for now. Just get your code right first, then see if you can improve on warnings if they matter (unless it is causing unacceptable speed then it doesn't matter in this case).
Your F_min I am a bit confused about as you should presumably have an if statement for checking the min against current min too if you want that.
If that is meant to be getting the minimum of all your maximums then it should be right at the bottom outside the loop as:
F_min = min( F_max );
Your code appears to be taking the maximum of your k's which is just an index not your values of interest. Also unless I am missing something you only need to keep a single F_new value (though you could simply use F(i,:) itself in your F_max line.
  9 Comments
Cat
Cat on 28 Oct 2014
Edited: Cat on 28 Oct 2014
I'm sorry I didn't see the edit in your last response as I happened to read it as you put it up! I have now put for my max/min section this, but unfortunately I am still getting the maximum value out as Inf at position 1.
[F_min, idx2] = min( F_max )
F ( F == Inf ) = NaN;
[F_max, idx2] = nanmax( F_max )
Can I input something like {below} to give out each of my I and j, rather than just one position (position 2 which is my column value)?
F ( F == Inf ) = NaN;
[F_max, idx1, idx2] = nanmax( F_max )
[F_min, idx1, idx2] = min( F_max )
I have tried this, but it isn't giving me the same min(or max) & the two positions, it is instead listing.
F ( F == Inf ) = NaN;
[F_max, idx1] = nanmax( F, [], 2 )
[F_max, idx2] = nanmax( F, [], 2 )
[F_min, idx1] = min( F, [], 2 )
[F_min, idx2] = min( F, [], 2 )
Adam
Adam on 28 Oct 2014
You shouldn't be passing F_max to nanmax, you should be passing F in.
nanmax (just like max) only returns indices in one direction, but it will return 1 index for each row - i.e. the index of the maximum value on each row.
[F_max, idx] = max( F, [], 2 );
[F_min, idxRow] = min( F_max );
idxCol = idx( idxRow );
That should give you the row index and the column index of the value you want in idxRow and idxCol.
Those values can then be used exactly as if they were i and j in your loop when it comes to retrieving the parameters for that particular value of F.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!