Beginner - Script to find max value in columns

2 views (last 30 days)
Ben
Ben on 22 Nov 2014
Edited: Star Strider on 22 Nov 2014
I'm trying to write a script. I have a 955*9 matrix, and I want to find which of the columns have values in excess of 3.5 -
%Script to analyse which profiles exceed 3.5G
%get size of G_force
[rows, columns]=size(G_force);
%Initialise result vector
DangerousG=0;
%Initialise counters
in=1;
for ic=1:columns
G=G_force(ic);
for ir=1:rows
G=G_force(ic);
if G>3.5||G<-3.5
DangerousG(in)=G_force(ic);
in=in+1;
end
end
end
fprintf('The profiles with dangerously high G-Force are \n')
fprintf('%d\n',DangerousG)
I managed it using this next bit of code, but I'd prefer to get it done in the above format -
g=G_force
[r,c]=find(g>3.5|g<-3.5);
subscr=[r c]

Answers (2)

Star Strider
Star Strider on 22 Nov 2014
You seem to have solved it to your satisfaction, so I’m at something of a loss as to what to suggest.
This will give your a logical vector with a 1 in the columns that have values in excess of 3.5Gz and the index of the first such value in each column.
Gz = randi([-4 4],15,9);
[extrmGz,idx] = max(abs(Gz)>3.5,[],1);
(The tolerable limits of Gz for humans are -3 to +6 without external compression equipment.)
  1 Comment
Star Strider
Star Strider on 22 Nov 2014
Edited: Star Strider on 22 Nov 2014
My pleasure.
If you want the number of profiles with dangerous Gz values, simply define:
DangerousG = sum(extrmGz);
with my code.

Sign in to comment.


Ben
Ben on 22 Nov 2014
The first script gives an output of
>> DangerG The profiles with dangerously high G-Force are 0
or whatever value I happen to use when I %Initialise result vector. Thanks for you help

Tags

Community Treasure Hunt

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

Start Hunting!