I need a little help with tables.
2 views (last 30 days)
Show older comments
The task wants a table in return, where:
-the first column shows the time,
-the second column shows numbers (from -5 to 5),
-the third column shows the grades (they are strings) determined by rounding the numbers in the second column
-and the columns are named: ido, eredmeny and ertekeles.
This is the code Ive found on one of the older sites with the same task, but it doesnt work.
function tbl = f91()
t = 0:0.001:pi;
for i=1:length(t)
switch round(abs(5*sin(3*exp(t(i)))))
case 1
et{i, 1} = 'elegtelen'
case 2
et{i, 1} = 'elegséges'
case 3
et{i, 1} = 'kozepes'
case 4
et{i, 1} = 'jo'
case 5
et{i, 1} = 'jeles'
otherwise
et{i, 1} = 'elegtelen'
end
end
tbl = table(t', 5*sin(3*exp(t')), et, 'VariableNames', {'ido', 'eredmeny', 'ertekeles'});
end
2 Comments
Dyuman Joshi
on 7 May 2022
It is working.
If the output is not what it is expected to be, mentioned that explicitly.
Use round while defining your table, round(5*sin(3*exp(t')))
t = 0:0.001:pi;
for i=1:length(t)
switch round(abs(5*sin(3*exp(t(i)))))
case 1
et{i, 1} = 'elegtelen';
case 2
et{i, 1} = 'elegséges';
case 3
et{i, 1} = 'kozepes';
case 4
et{i, 1} = 'jo';
case 5
et{i, 1} = 'jeles';
otherwise
et{i, 1} = 'elegtelen';
end
end
tbl = table(t', 5*sin(3*exp(t')), et, 'VariableNames', {'ido', 'eredmeny', 'ertekeles'})
Answers (2)
Sayan
on 25 Sep 2023
Edited: Sayan
on 22 Nov 2023
Hi Tamás Zsombor Tolvaj,
I understand from your issue that column 2 of your table with the name "eredmeny" should be within the desired value, i.e., -5 to 5 in the table.It is showing the same in your code too.
However, you can follow the below code snippet as an alternative to find the same.
%you can randomly generate the uniformly distributed values
%for t in the range of (-pi/2 to pi/2)
t=-pi/2+rand(1,N)*pi %Here N data points are taken,you can change the value as per requirement
%run the other part of the code as mentioned in the question
%Change the values of second column using this new equation to find the
%range of -5 to 5
tbl.eredmeny=(5*sin(t))';
You can further find more information on "table" and "rand" function in the following MATLAB documentation
- "table": https://www.mathworks.com/help/matlab/ref/table.html
- "rand":https://www.mathworks.com/help/matlab/ref/rand.html
Hope this helps you in resolving the issue.
0 Comments
Image Analyst
on 25 Sep 2023
Edited: Image Analyst
on 25 Sep 2023
Like some of the others said, the code is working in that it does give numbers in column 2 in between -5 and 5 (if that's what you meant by "not working"). However it can be improved in several ways, and I do that here:
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
tic
tbl = f91();
toc
% Find range of the sin formula in column 2
plot(tbl.eredmeny, 'b-')
title('tbl.eredmeny', 'FontSize',fontSize);
grid on;
yline(0, 'LineWidth',3); % Draw a y axis.
% Print out the max and min
fprintf('Max Value = %f, min value = %f.\nNumber of rows in table = %d\n', max(tbl.eredmeny), min(tbl.eredmeny), height(tbl));
%----------------------------------------------------------------------------------------------------------
function tbl = f91()
t = linspace(0, pi, 3142);
numRows = numel(t);
eredmeny = 5 * sin(3 * exp(t(:)));
et = cell(numRows, 1);
for k = 1 : numRows
switch round(abs(eredmeny(k)))
case 1
et{k} = 'elegtelen';
case 2
et{k} = 'elegséges';
case 3
et{k} = 'kozepes';
case 4
et{k} = 'jo';
case 5
et{k} = 'jeles';
otherwise
et{k} = 'elegtelen';
end
end
tbl = table(t(:), eredmeny, et, 'VariableNames', {'ido', 'eredmeny', 'ertekeles'});
end
However if by "not working" you meant something else, say what it is. Did you call the function from a script, or did you just click the green run triangle?
Perhaps the numbers from -5 to 5 should be generated by rand rather than that trig function you used.
eredmeny = -5 * 10 * rand(numRows, 1);
Did your task say specifically to use that, or was it just there from that other code you copied?
0 Comments
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!