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
I want to calculate the Euclidean distance between each pair of num1, num2 and center1 but an error is shown: "double Conversion to double from cell is not possible"
[num1]={4,4,4,4,43,4,34,55,6,6,6,65,5,4,4,43,2,2,3,45,6,67,7,7,7,7,4,5,66,5,4,3,3,2,3,4,5};
[num2]={41,42,43,44,43,4,3,5,62,62,63,65,54,4,4,4,24,24,34,4,6,6,47,47,7,7,4,45,16,51,41,13,3,2,3,4,5};
[center1]={20,30};
Create an array like this:
a = [1 2 3 4];
Using curly braces like you did it creates a cell array.
To get the distance, you may use the MATLAB function pdist:
D = pdist(X)
This computes the Euclidean distance between pairs of objects in m-by-n data matrix X.
To calculate the Euclidean distance between two vectors:
a = [1 2 3 4];
b = [1 2 4 4];
d = pdist([a;b])
For further information, refer to the documentation.
The pdist command requires the Statistics and Machine Learning toolbox. If you don't have that toolbox, you can also do it with basic operations. Euclidian distance between two vectors of points is simply the sqrt(sum( (a-b).^2 ). So, you can do:
a = [1 2]; %2D vector, though any dimension is OK
b = [4 7]; %any values, but must be same size as `a`
dist = sqrt(sum((a-b).^2)); %Euclidian distance
No toolboxes needed!
As mentioned in the other answer, don't use curly braces to enclose your numbers. This is a case where you need plain matlab arrays, which are the square brackets as used above.
Related
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 4 years ago.
Improve this question
I have 1D vector. For example: y=[0.2 0.9 1.0 1.0]. I can plot it with plot(y) to get a graph of y(x) where x values are just indices [1, 2, 3, 4].
Now, instead of x values being just indices, I want to map them to [0,1] range: x = linspace(0,1,length(y)). I get: x=[0 0.3333 0.6667 1.000].
I can now make a graph with plot(x,y):
Now, however, I want an inverse graph, so I make a plot with plot(y,x):
I want to be able to now use plot(x) to get the same shape as above. However, if I use plot(x), as expected, I just get a straight line.
How to transform x in such a way that plot(x) will give the same shape as plot(y,x)?
Upd.: If I try just 1./x:
I have managed to find a solution, so for anybody who also need its:
x = linspace(0,1,length(y));
% not needed in this toy example, but can be required for a bigger vector:
[y_unique, idx] = unique(y);
inv_y = interp1(y_unique,x(idx),x);
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 4 years ago.
Improve this question
I am new to MATLAB and I want to create a random n*n matrix containing just -1 OR 1 as values.
any help ?
I would use randi
% Generate random array of 0s and 1s, *2 and -1 to give random values -1 or +1
m = randi([0,1], n)*2-1
See also: introductory docs on random integers.
I typically use randi to generate the indexes of the numbers I am interested in. E.g. in your case you are interested in the numbers
a= [-1,1];
Thus we use
b = randi(length(a),2,2); %Generate matrix of size 2x2
to generate a random set of indexes. Finally we simply convert the indexes to the numbers of interest.
c = a(b); %Now a 2x2 matrix of -1, 1 numbers
A=rand(n);
thres=rand(1); % or whatever percentage
A=A>thres; % 1 and 0
A(A==0)=-1; % makes 0 -1
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;
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.
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