matlab coding question help~

조회 수: 5 (최근 30일)
ONION LIM
ONION LIM 2013년 10월 8일
댓글: Les Beckham 2022년 12월 1일
hallo, i got some question here. my question is i need to read some data from a notepad file then use the data to continue the equation... example, i created a notepad, inside the notepad is
1 2
3 4
5 6
then i use
T=importdata('trial.txt')
U1=T(1,1)
U2=T(1,2)
U3=T(2,1)
U4=T(2,2)
U5=T(3,1)
U6=T(3,2)
then put inside an equation
V1=U1+1
V2=U2+1
V3=U3+1
V4=U4+1
V5=U5+1
V6=U6+1
is there any shorter code can use??? this seem like very long... and when the number of data in the notepad change then everything cant use, any idea?
  댓글 수: 3
Krishanth
Krishanth 2022년 12월 1일
If a second order polynomial is written in the general form:
then the roots (i.e. the values of x that satisfy the equation) can be determined using the quadratic formula:
Code has already been provided to define a function named quadraticSolver that accepts a 3-element row vector and assigns it to the input variable abc, with values for a, b, and c in the first, second, and third column respectively. Add commands to do the following:
1. Check if the roots of the polynomial defined by a, b, and c are real and distinct. Note that the roots will be real if the value under the square root is greater than or equal to zero. They will not be distinct, however, if the value under the square root is exactly equal to zero. Assign an an integer (uint8 datatype) value to the output variable rootCondition as follows:
  • The value 2 if the roots are real and distinct
  • The value 1 if the two roots are the same.
  • The value 0 if the roots are not real (i.e. complex because the value under the square root is less than 0)
2. Assign the solution to the quadratic to the output variable rootValues according to the following guidelines:
  • In cases where there are two real and distinct roots, compute the two roots and assign them in ascending order to a two-element row vector.
  • In cases where the two roots are the same, compute the root and assign this value to both elements in a two-element row vector.
  • In cases where the roots are complex, output assign the text 'complex roots' output variable rootValues.
Note the value of abc is defined as a function input. Do not overwrite this value in your code. Be sure to assign values to each of the function output variables.
Use an if-elseif-else structure in your code.
Les Beckham
Les Beckham 2022년 12월 1일
Why did you post your homework assignment as a comment responding to a completely unrelated 9 year old question?
If you have a specific Matlab related question, post it as a new question and show what you have tried so far (your Matlab code), and what isn't working as you expect.

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

답변 (1개)

Cedric
Cedric 2013년 10월 8일
편집: Cedric 2013년 10월 8일
Convert T into a vector with correct dimension and ordering, and then work in a vector way (I removed the ';' at the end of each line so you can see the output):
>> T = reshape(T.', [], 1)
T =
1
2
3
4
5
6
>> V = T + 1
V =
2
3
4
5
6
7
  댓글 수: 2
ONION LIM
ONION LIM 2013년 10월 8일
if say my equation is not V = T + 1, but is like
V1=U1+U2
V2=U2+U3
V3=U3+U4
V4=U4+U5
V5=U5+U6
V6=U6+U1
then how should i write it??
Cedric
Cedric 2013년 10월 8일
편집: Cedric 2013년 10월 8일
Several possibilities, one is simply
V = T + [T(2:end); T(1)] ;
where the 2:end range of indices works whatever the length of T (which is not hardcoded). More generally, the tools that you have at hand are: indexing, array concatenation [], and functions for array manipulation like RESHAPE, FLIPLR, FLIPUD, VERTCAT, HORZCAT, CIRCSHIFT, etc.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by