MATLAB Answers

Subfunction equations applied to a main function matrix

5 views (last 30 days)
lauuser1
lauuser1 on 9 Feb 2016
So I've made a 5x1 vector in my main function. Let's call this vector V and let's just say the numbers in the vector are 2, 3, 4, 5, and 6. Now in my sub-function I have two equations, one for X and one for Y. Let's say
X = 2.*T.*V
Y = 0.5.*T.^2
I also have a T column vector that is let's say 10x1. So now I have to create two new matrix that have the same amount of rows as the T vector along with 5 columns that associate with the 5 numbers in vector V. Basically, new matrix X_MATRIX that pertains to equation X should have a 10x5 matrix where each value in vector V is applied to the equation. So if the second row of vector T is 5 and we use the first number in vector V, which is 2, the second row and first column value of X_MATRIX should be 2.*5.*2 which is 20. How would I code that to work for every value so the whole X_MATRIX has correct values? I assume the same thing will be applied to Y_MATRIX that pertains to the equation Y, however since Y doesn't have V in it will it be the same?

  0 Comments

Sign in to comment.

Answers (1)

Subin Kuttappan Stellal Mary
Hi Lavy,
Since T is 10x1 matrix and V is 5x1 matrix, the following line of code does now work :
X = 2.*T.*V
For the above code to work, T and V should be of same size.
I understand your use case as follows :
The output required is X_MATRIX and Y_MATRIX.
X_MATRIX is a 10x5 matrix, where each row contains the product of the row element from T and all the elements from V (i.e. 5 elements). Y_MATRIX is a 10x1 matrix.
Following lines of code will perform this :
X_MATRIX = 2*T*V'
Y_MATRIX = 0.5.*T.^2
Sample I've tried :
V = [2 3 4 5 6]';
T = (1:10)';
X_MATRIX = 2*T*V'
X_MATRIX =
4 6 8 10 12
8 12 16 20 24
12 18 24 30 36
16 24 32 40 48
20 30 40 50 60
24 36 48 60 72
28 42 56 70 84
32 48 64 80 96
36 54 72 90 108
40 60 80 100 120
Y_MATRIX = 0.5.*T.^2
Y_MATRIX =
0.5000
2.0000
4.5000
8.0000
12.5000
18.0000
24.5000
32.0000
40.5000
50.0000

  0 Comments

Sign in to comment.


Translated by