This question already has answers here:
i want to find the location of a number in a matrix in matlab
(2 answers)
Closed 9 years ago.
Please help me I want to find all zero elements in matrix in MATLAB.
For example, if matrix
A = [1 3 0; 2 4 9; 2 0 7]
the answer should be (1,3) and (3,2).
But the real matrix I want to solve is very big. Is there any way to do this.
use find:
[r c] = find(A==0);
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 calculate sample and population variances in Matlab?
(2 answers)
Closed 6 years ago.
I don't understand why MATLAB does not normalize the data by its size while computing the variance. Ex :
a=[1 2 3];
var(a); %// 1
However we know that the fundamental definition of the variance is :
variance(a) = ((a(i)-mean(a))^2) / size(a) %// here size(a)=3
%//this formula gives a variance equal to 2/3
any idea?
It's explained in the docs:
MATLAB is uses the formula for unbiased sample variance by default
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 answers here:
applying norm function to rows of matrix - Matlab [duplicate]
(3 answers)
Closed 9 years ago.
I have a Nx3 matrix (A) the columns are X,Y,Z respectively. I want to calculate the norm that is sqrt(X^2+Y^2+Z^2) for each row. I did a for loop for that:
for i = 1:length(A)
Result(i) = norm(A(i,:))
end
is there any other way to do it avoiding for loop?
Thanks
You can do it like this:
sqrt(sum(A.^2, 2))
Your method returns a 1x3 where this returns a 3x1. So if you want you can transpose it but I doubt you really need to.
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