Clear Filters
Clear Filters

Appending an asterisk to my output matrix if x > y

3 views (last 30 days)
I have a problem with my current code where I have to append an asterisk to an extra collumn of a matrix for every row in the matrix if the value in the third collumn of the matrix is greater than the average of each value in the third collumn. I cannot figure out the exact way to do it and I was hoping someone could provide the code for how they would go about solving it.
here is my current code
clc, clear all
format short
% ***** CONSTANT *****
FILENAME = 'dragCoef06.txt';
HORIZONTALFORCE = 350; % Newtons
AIRDENSITY = 1.225; % kg/m^3
VELOCITY = 25; % m/s
NUMCARS = 10;
NUMCOLS = 2;
AST = '*';
% does file exist?
if ~exist( FILENAME, 'file' )
disp ( 'File not avaliable' )
else
% file exist contiune
% ***** INPUT *****
% draw year and coef from file
fileID = fopen ( FILENAME );
% ***** COMPUTE *****
% compute the frontal area all cars
dragtemp=fscanf(fileID,'%f');
k=1;
for r = 1:NUMCARS
for c = 1:NUMCOLS
k=k+1;
dragMatrix06(r,c) = dragtemp(k);
end
end
frontalArea = ( 2.*HORIZONTALFORCE )./ ( AIRDENSITY.*VELOCITY^2.*(dragMatrix06));
averageArea = mean(frontalArea(:,2))
% add frontal area as third column of matrix
newMatrix = [ dragMatrix06, frontalArea, ];
newMatrix(:,3)=[];
% ***** OUTPUT *****
% print table using fprintf()
fprintf ( '%21s \n', 'All Cars' )
fprintf ( '%9s', 'Year' )
fprintf ( '%10s', 'Drag' )
fprintf ( '%11s \n', 'Frontal' )
fprintf ( '%19s', 'Coef' )
fprintf ( '%13s \n', 'Area(m^2)' )
fprintf ( '%9.0f %9.2f %9.4f \n', [newMatrix]')
fclose ( fileID );
end
my desired output is
All Cars
Year Drag Frontal
Coef Area(m^2)
2015 0.24 3.8095 *
2016 0.27 3.3862 *
2016 0.36 2.5397
2016 0.39 2.3443
2017 0.22 4.1558 *
2017 0.23 3.9752 *
2018 0.26 3.5165 *
2018 0.33 2.7706
2018 0.45 2.0317
2019 0.28 3.2653 *
* Frontal area above area
but my current output is
All Cars
Year Drag Frontal
Coef Area(m^2)
2015 0.24 3.8095
2016 0.27 3.3862
2016 0.36 2.5397
2016 0.39 2.3443
2017 0.22 4.1558
2017 0.23 3.9752
2018 0.26 3.5165
2018 0.33 2.7706
2018 0.45 2.0317
2019 0.28 3.2653

Answers (1)

James Tursa
James Tursa on 5 Mar 2020
Add the ASCII codes for space and asterisk in the 4th column and print that column as a string. E.g.,
newMatrix(:,4) = ' ';
newMatrix(frontalArea>averageArea,4) = '*';
:
fprintf ( '%9.0f %9.2f %9.4f %s\n', [newMatrix]');
  2 Comments
Thomas Smith
Thomas Smith on 5 Mar 2020
Im still having issues
This is my new code
clc, clear all
format short
% ***** CONSTANT *****
FILENAME = 'dragCoef06.txt';
HORIZONTALFORCE = 350; % Newtons
AIRDENSITY = 1.225; % kg/m^3
VELOCITY = 25; % m/s
NUMCARS = 10;
NUMCOLS = 2;
AST = '*';
% does file exist?
if ~exist( FILENAME, 'file' )
disp ( 'File not avaliable' )
else
% file exist contiune
% ***** INPUT *****
% draw year and coef from file
fileID = fopen ( FILENAME );
% ***** COMPUTE *****
% compute the frontal area all cars
dragtemp=fscanf(fileID,'%f');
k=1;
for r = 1:NUMCARS
for c = 1:NUMCOLS
k=k+1;
dragMatrix06(r,c) = dragtemp(k);
end
end
frontalArea = ( 2.*HORIZONTALFORCE )./ ( AIRDENSITY.*VELOCITY^2.*(dragMatrix06));
averageArea = mean(frontalArea(:,2))
% add frontal area as third column of matrix
newMatrix = [ dragMatrix06, frontalArea, ];
newMatrix(:,4) = ' ';
newMatrix(frontalArea>averageArea,4) = '*';
% ***** OUTPUT *****
% print table using fprintf()
fprintf ( '%21s \n', 'All Cars' )
fprintf ( '%9s', 'Year' )
fprintf ( '%10s', 'Drag' )
fprintf ( '%11s \n', 'Frontal' )
fprintf ( '%19s', 'Coef' )
fprintf ( '%13s \n', 'Area(m^2)' )
fprintf ( '%9.0f %9.2f %9.4f %s\n', [newMatrix]')
fclose ( fileID );
end
and this is the new output
averageArea =
3.1795
All Cars
Year Drag Frontal
Coef Area(m^2)
2015 0.24 0.0005 ߠ
0 0.00 32.0000 ߠ
0 0.00 32.0000 ߠ
0 0.00 32.0000 ߡ
0 0.00 32.0000 ߡ
0 0.00 32.0000 ߢ
0 0.00 32.0000 ߢ
0 0.00 32.0000 ߢ
0 0.00 32.0000 ߣ
0 0.00 32.0000 * * * * * *
James Tursa
James Tursa on 5 Mar 2020
Sorry, try rows:
newMatrix(4,:) = ' ';
newMatrix(4,frontalArea>averageArea) = '*';

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!