필터 지우기
필터 지우기

I need a little help with tables.

조회 수: 1 (최근 30일)
Tamás Zsombor Tolvaj
Tamás Zsombor Tolvaj 2022년 5월 7일
편집: Sayan 2023년 11월 22일
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
Chuguang Pan
Chuguang Pan 2022년 5월 7일
It can work.
Dyuman Joshi
Dyuman Joshi 2022년 5월 7일
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'})
tbl = 3142×3 table
ido eredmeny ertekeles _____ ________ _____________ 0 0.7056 {'elegtelen'} 0.001 0.69074 {'elegtelen'} 0.002 0.67586 {'elegtelen'} 0.003 0.66096 {'elegtelen'} 0.004 0.64603 {'elegtelen'} 0.005 0.63109 {'elegtelen'} 0.006 0.61612 {'elegtelen'} 0.007 0.60114 {'elegtelen'} 0.008 0.58613 {'elegtelen'} 0.009 0.5711 {'elegtelen'} 0.01 0.55606 {'elegtelen'} 0.011 0.54099 {'elegtelen'} 0.012 0.5259 {'elegtelen'} 0.013 0.5108 {'elegtelen'} 0.014 0.49567 {'elegtelen'} 0.015 0.48052 {'elegtelen'}

댓글을 달려면 로그인하십시오.

답변 (2개)

Sayan
Sayan 2023년 9월 25일
편집: Sayan 2023년 11월 22일
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
Hope this helps you in resolving the issue.

Image Analyst
Image Analyst 2023년 9월 25일
편집: Image Analyst 2023년 9월 25일
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
Elapsed time is 0.035508 seconds.
% 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));
Max Value = 4.999996, min value = -5.000000. Number of rows in table = 3142
%----------------------------------------------------------------------------------------------------------
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?

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by