Appending an asterisk to my output matrix if x > y
3 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
回答 (1 件)
James Tursa
2020 年 3 月 5 日
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 件のコメント
James Tursa
2020 年 3 月 5 日
Sorry, try rows:
newMatrix(4,:) = ' ';
newMatrix(4,frontalArea>averageArea) = '*';
参考
カテゴリ
Help Center および File Exchange で Convert Image Type についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!