This question already has answers here:
Matlab Special Matrix
(2 answers)
Closed 10 months ago.
I want generate matrix of the form:
1 2 3
2 3 4
3 4 5
using MATLAB.
I can make code to generate matrix above:
for i=1:3
for j=1:3
idx(i,j)=i+j-1;
end
end
But, I want use MATLAB built-in function to generate matrix above to simplify my code (I don't want using looping again). Anyone know MATLAB built-in function to generate matrix above?
In MATLABĀ® R2016b and later:
[1:3] + [0:2]'
Old style:
bsxfun(#plus,1:3,[0:2]')
Try using
[1:3; 2:4; 3:5]
This generates a 3 x 3 matrix and simplifies your code down to one line.
Related
This question already has answers here:
Multiply a 3D matrix with a 2D matrix
(10 answers)
Closed 5 years ago.
I try to use svd in my work which will return the vector as below.
A=rand(10,5);
b=rand(10,5,100);
[U, S , V]=svd(A);
What I'm trying to do is to multiply each slice of b with U. With one specific slice, this operation is valid:
c=U'*b(:,:,1);
However when I try to use a vectorized method as below, it return array dimensions mismatch error.
Utran=U.';
c = cellfun(#(x) x.*b,num2cell(Utran,[1 2]),'UniformOutput',false);
I could probably use loop for the first method, but it's not efficient if I have a large matrix. Any idea what's my mistake here?
The following is a vectorized solution.
Not sure if it's faster than using loops; and even if it is, note that it uses more memorry for intermediate computations:
result = permute(sum(permute(conj(U), [1 3 4 2]).*b), [4 2 3 1]);
This question already has answers here:
How to have square wave in Matlab symbolic equation
(3 answers)
Closed 6 years ago.
I have a vector T that is defined as
T=zeros(1,4)
I want to define T such that T(1) and T(2) are equal to 1 and T(3) and T(4) are equal to 0. So that when I plot T it looks like a square wave.
I have tried
for i=1:2:size(T,2)
T(i:i+1)=1
end
figure; plot(T);
But this does not give the desired result. It turns out to be [1,0,1,0].
What is the right way to do this assignment?
To differentiate from questions about plotting square waves:
I wanted to find out how exactly to create the loop that would plot to look like a square wave, without explicitly defining frequency or using the symbolic equation. I would then use this information to modify another script that would do the same thing but a larger vector T where the "period" is not the same. Sometimes it is 11s, sometimes 9s and so on.
The period is 4, not 2:
for i=1:4:size(T,2)
T(i:i+1)=1
end
figure; plot(T);
If you have access to the signal processing toolbox, an alternative is using the square function:
T = (1+square(0:pi/2:3*pi/2))/2 %// 1 1 0 0
This question already has answers here:
Reshaping of Array in MATLAB
(3 answers)
Closed 7 years ago.
I have the following matrix:
50,60,55,67,70
62,65,70,70,81
72,66,77,80,69
I turn now the matrix into a vector but in row-major. This gives the following vector:
50,60,55,67,70,62,65,70,70,81,72,66,77,80,69
Now I would like to turn this vector into the same matrix as above. The problem is that reshape(matrix,[3,5]) does not work because Matlab operates column-major.
How can this be done efficiently (for large matrices)?
To solve this, use
reshape(matrix,[5,3]).'
First using reshape with row and column dimension swapped, you get a matrix with the right order but transposed, then using transpose you get the right output.
Having the control systems toolbox, you could also use vec2mat
This question already has an answer here:
MATLAB one liner for batch assignment in 2D matrix?
(1 answer)
Closed 7 years ago.
So I'm a beginner in MATLAB so this question might be trivial.
Suppose x=[1 2 3 4 5] and y=[3 4 2 5 1] and img = zeros(5,5). I want to set img(1,3),(2,4),(3,2),(4,5),(5,1) to 1. How do I do this? When I simply try img(x,y), it takes all the combinations of indices like (1,3),(1,4),(1,2) etc. which is not what I want.
As you have experienced, MATLABs indexing doesn't work that way. To get a feeling for the ways indexing works in MATLAB, please have a look at this nice article from Mathworks.
Now how to tackle your problem: The solution is to use linear indexing. You can always index a 2-dimensional matrix either by (i,j) or with a linear index k which increases column-wise. You can convert between matrix-indexes and linear indexes using the sub2ind function. To get the correct indexes for your questions, use
img = zeros(5,5)
x = [1 2 3 4 5];
y = [3 4 2 5 1];
ind = sub2ind(size(img),x,y);
img(ind) = 1;
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How do I divide the rows of a matrix by different values in MATLAB (array division)
I have a matrix A (size MxN) in Matlab and a Vector b with M rows and now I want to divide all elemtens in the i-th row of A by the i-th entry in b like a(i,:)/b(i) but I really don't want to use this sort since I than use a for-loop and I definitly need a FAST solution!
Could anybody help out? Thanks!
Edit: Somehow I just came up with it after posting... My solution is bsxfun(#rdivide, [1 1; 2 2; 3 3], [2 2 6]'), do you think that's a good and fast one?
You want to use bsxfunc :
bsxfun(#rdivide,A,B)
http://www.mathworks.com/help/techdoc/ref/bsxfun.html