MATLAB create a matrix M = F(i,j) [closed] - matlab

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Suppose I have a function of 2 variables F(i,j) which depends on the row index and column index of a matrix, and I want to fill the matrix with the values M_ij = F(i,j)
Of course it's possible to do a loop through i and j, or even only i or j if the function F can be vectorized, but I'd like to know the neat way to do that.

It's impossible to answer without seeing your F but let's assume that F is vectorized such as
F = #(x,y)x+y;
then you could use ndgrid:
[I,J] = ndgrid(1:m,1:n);
M = F(I,J)
In the above case, and this may well apply in your case as well, you might be able to vectorize the function directly using something like bsxfun:
M = bsxfun(#plus, 0:m-1, 1:n);

Regardless of whether your function F is vectorized or not, you have to evaluate it for every value of i and j. If F is not vectorized, you will have to do a loop over the indices manually. If F is based on MATLAB builtins like sin, log, etc., it is most likely vectorized. In this case, you can pass in i and j that are the same size as M and get the result in one step:
[j, i] = meshgrid(1:size(M, 2), 1:size(M, 3))
M = F(i, j)
Note that meshgrid takes and returns the parameters as X, Y, which is the opposite of matrix indexing order row, col.

Related

How to put trigonometric numbers as a power in matlab [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to make a graph with this function in matlab in space [-2,2]. Because I am new (really new actually) in matlab, I am struggling, especially with the first part of the function, the one with the "e":
So any help about the whole thing will be very useful.
Let's cover all the points in no particular order:
The e is known as Euler's constant, where e ~ 2.71828...
Something like ex, i.e. raising e to the power of x is known as the exponential function. While you could in theory compute e and then use the power operator (^ in matlab) to raise e to that power accordingly, this is in fact a less precise way of calculating the exponential function, and therefore matlab provides the exp function for that purpose. If you pass an array [x1, x2,...] to exp, it will perform this function "elementwise", and return [ex1, ex2,...] appropriately.
The sin function, similarly, if given an array of numbers, will calculate the sin for each of those numbers, and return an array of the same shape as its input.
You could use fplot with an anonymous function as Neo suggested, but I find that beginners find that confusing. So instead I would suggest you create an array of values between [-2,2], and obtain the value of y for each of them, which in matlab can be done as a single operation because it's good at working with arrays and performing such 'vectorized' operations:
x = [-2:0.1:2]; % Create an array of values from -2 to 2, with a step of 0.1
% Note: the ';' at the end suppresses output; if you want to
% see the contents of your array, remove it at the end.
Now that you have your array x you can perform operations on it. By convention, matlab uses "dot-operators" to denote "elementwise" operations, as opposed to 'undotted' ones denoting primarily "matrix" operations. Therefore to raise all elements of the array x to the power of 6, you would do x .^ 6
With that in mind, you can now calculate your f(x) for each point in the array x, by using elementwise operations:
y = exp( sin(x).^3 ) + x.^6 - 2*(x.^4) - x.^3 - 1;
The result is an array y of the same size as x.
You can now plot this using the plot command, which takes two arrays of the same size and plots all points in the first array against their equivalent points in the second array, as (x,y) pairs:
plot( x, y );
Output:
As you can see, matlab 'connects' the points by default. If you wanted to see just the individual points of your array instead, you can specify this as the third argument:
plot(x, y, 'o');
Type help plot at the matlab terminal to see more options for the plot command.
PS: The above plots were made in octave rather than in matlab, because I don't have matlab at home. Your own plots may look slightly different.
For a simple one-liner you could use:
fplot(#(x) exp(sin(x).^3) + x.^6 - 2*x.^4 - x.^3 - 1, [-2 2]);
See fplot.

Matlab: equivalent of R's matrix multiplication (A %*% B)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Working in Matlab with time-homogenous Markov Chains and looking to figure out how I can perform matrix multiplication in Matlab for matrix A, similar to R's matrix multiplication, i.e., A %*% A. It would be even better if I could perform A^n instead for a given n instead of having to use A %*% A %*% A, when n = 3, for example.
Any help is greatly appreciated!
First of all you can raise a Matrix to a power in MATLAB:
A ^ n = A * A * A * ... * A
Actually MATLAB uses pretty sophisticated algorithms behind the scene to accelerate this.
For example, if the Matrix is diagonalizable, MATLAB will use that to accelerate the calumniation.

Adding Matrices in Matlab with out looping [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
how to sum m-by-n matrix A, to m by n by p matrix B, using Matlab without a For loop. Where the result C should be m by n by p matrix, the direct addition results in
Error using +
Matrix dimensions must agree.
?
If you are looking to add A to each of the p slices of B then you should use bsxfun:
bsxfun(#plus,A,B)
First modify A to have the same size of B by replicating A, p times:
A = repmat(A ,[1 1 p]);
Now A is m by n by p the summation then can be done as
C = A + B
Where C is a m by n by p matrix

Octave beginner help. Creating a function that takes a component matrix as a parameter and returns a matrix? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to create a function that takes a component matrix as a parameter and returns a matrix?
Apparently this function should normalise my data?
There are other instructions along with this step in my project such as:
Take the matrix and calculate the mean value along a certain column.
Calculate the difference between the measurement and this mean.
Subtract this difference from each measurement.
Return corrected matrix to the script.
Place corrected matrix in a variable within the script.
(I don't know if this is what the function is supposed to do or anything I'm completely lost and any help would be appreciated thanks!)
This is probably homework but I'll help you get started.
To create a function which takes a matrix and return a matrix:
function m_out = my_function(m_in)
%insert calculations here
end
To find the 2-norm of a matrix (which is the largest singular value):
the_norm = norm(my_matrix); % returns a scalar, 2-norm of matrix
To find the mean of a vector:
the_mean = mean(my_vector); % returns a scalar, mean of the vector
To access a specific column of a matrix:
my_col = my_matrix(:, col_number); % my_col is a vector
To access a specific row of a matrix:
my_row = my_matrix(row_num, :); % my_row is a vector
To subtract a scalar (single number) from a matrix:
new_matrix = old_matrix - single_number; % returns a matrix
To store a matrix into a variable (example):
my_matrix = [1,2,3;4,5,6;7,8,9];
Give it a try creating a function which puts this all together.

AX=0 and introducing error matrix [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm having a matrix of 10 rows and 5 columns, thatr I have called A
I would like to make AX=0 and determine X which contains 5 unknown parameters.
What I did is
null(A)
But it seems that it's considering what I did as a linear algebra.
I would like to introduce another matrix which contains a matrix of errors . which would give me a priori:
AX + E = 0
because the results are not accurate, indeed, but still, I would like to find the closest parameters of the unknown vector (X) and get an error matrix.
Can you help me with that?
You can do eigen value decomposition of A^T.
[Q, D] = eigen(A^T)
And take the eigen vectors that corresponds to the smallest eigen value.
That is take y vectors from the right hand side of the Q matrix.
y is the column number of X.
And then E = -AX
====edit=====
OK. actually you want to minimize E. But since E is a matrix I assume you want to minimize the sum of squares of all the elements in E.
That is:
||E||_F^2 = ||-AX||_F^2 = trace(X^T(A^TA)X)
You can do eigen decomposition of A^TA
[Q, D] = eigen(A^TA)
QA^TA = DQ => D = QA^TAQ^T
To make it smaller, X should be the columns of Qs that corresponds to the smallest eigen value in the diagonal of D.
If there is a eigen value equals to 0, then AX can be 0 and E=0