I want to calculate the correlation coefficient between variables in MATLAB. I have a matrix called M, in which each column represents a variable. This is the function I'm using:
A = corrcoef(M,"Rows","complete")
I am getting a table of correlation coefficients, but not the variable names. For instance, I cannot directly see what the 3rd column in A represents - I'd have to go and check M to see the ordering of the variables along the columns.
How do I display the variable names in the correlation table?
Note: I converted M to a table and added variable headers, but the correlation table does not show the variable names.
As #BillBokeey suggested in the comments, a solution is to create a table from the array A, and then add the row and column headers I had previously created for M.
Related
Question
If there is a matrix of MxN, how can extract all of the data based in the columns?
Im interesting to pass each column to a function and to be plotted.
if A(:) is used all the matrix is merged into a single column, (I remember this command is intended for that) but this does not serve to me.
Matlab arrays use the indexing, partOfArray = array(rows, columns). The variables rows and columns can be a vector (including a scalar, which is a vector of length 1) or : which is effectively interpreted by Matlab as meaning 'all' (e.g. array(:,columns) would be all rows of the selected columns).
Alternatively, Matlab also allows linear indexing, in which array(aNumber) counts in order from array(1,1) first by rows, then by columns. e.g. if array is 2x4, array(5) is equivalent to array(2,1). When you call A(:) Matlab interprets that as using linear indexing to access all elements in the array, hence merging the matrix into a single column.
To access each column vector in a for loop, in this case printing it out, use:
A = magic(4)
numColumnsInA = size(A,2);
for i=1:numColumnsInA
disp(A(:,i))
end
You can find more information about indexing in Matlab here: Array Indexing
The following is a problem from an assignment that I am trying to solve:
Visualization of similarity matrix. Represent every sample with a four-dimension vector (sepal length, sepal width, petal length, petal width). For every two samples, compute their pair-wise similarity. You may do so using the Euclidean distance or other metrics. This leads to a similarity matrix where the element (i,j) stores the similarity between samples i and j. Please sort all samples so that samples from the same category appear together. Visualize the matrix using the function imagesc() or any other function.
Here is the code I have written so far:
load('iris.mat'); % create a table of the data
iris.Properties.VariableNames = {'Sepal_Length' 'Sepal_Width' 'Petal_Length' 'Petal_Width' 'Class'}; % change the variable names to their actual meaning
iris_copy = iris(1:150,{'Sepal_Length' 'Sepal_Width' 'Petal_Length' 'Petal_Width'}); % make a copy of the (numerical) features of the table
iris_distance = table2array(iris_copy); % convert the table to an array
% pairwise similarity
D = pdist(iris_distance); % calculate the Euclidean distance and store the result in D
W = squareform(D); % convert to squareform
figure()
imagesc(W); % visualize the matrix
Now, I think I've got the coding mostly right to answer the question. My issue is how to sort all the samples so that samples from the same category appear together because I got rid of the names when I created the copy. Is it already sorted by converting to squareform? Other suggestions? Thank you!
It should be in the same order as the original data. While you could sort it afterwards, the easiest solution is to actually sort your data by class after line 2 and before line 3.
load('iris.mat'); % create a table of the data
iris.Properties.VariableNames = {'Sepal_Length' 'Sepal_Width' 'Petal_Length' 'Petal_Width' 'Class'}; % change the variable names to their actual meaning
% Sort the table here on the "Class" attribute. Don't forget to change the table name
% in the next line too if you need to.
iris_copy = iris(1:150,{'Sepal_Length' 'Sepal_Width' 'Petal_Length' 'Petal_Width'}); % make a copy of the (numerical) features of the table
Consider using sortrows:
tblB = sortrows(tblA,'RowNames') sorts a table based on its row names. Row names of a table label the rows along the first dimension of the table. If tblA does not have row names, that is, if tblA.Properties.RowNames is empty, then sortrows returns tblA.
I've been trying to simulate a chemical reactor on Matlab. For this to work I need to be able to get some values from excel tables with thermodynamic properties. Each table is for a chemical species and the first row is always the temperature.
I would like to input the temperature so that Matlab would search the temperature column for the value and return, for example, the associated enthalpy.
I used xlsread to load the tables as matrices but I am having trouble to tell Matlab what to do next.
I've been told to try
[row, ~] = find(temp<=data(:,2)); %temp is the given temperature
but this just returns me
row = 1, 2, ...
I would also like to be able to handle intermediate temperatures that are not explicit in the table, it could be some kind of interpolation or just getting the nearest value.
Any help will be truly appreciated.
I have an NxM matrix where I am binning on the first column into n buckets. What I would like to know is if I would like to take the average of all values in the secondary columns of my matrix is this possible.
So basically what I want is if I have a 2x1000 matrix where I bin on the data for the first column, I'd like to be able to average all values in each bin based on the second column of my matrix.
Perhaps a function exists that will easily allow me to do this. I appreciate any input.
The question is:
How can I change an element in a matrix in Maple?
Is it possible?
Yes, it is certainly possible. (It's one of the primary properties of a Matrix, it is a mutable data structure. ie. its entries can be changed.)
If you have assigned a Matrix to the name A, then you can change the entry in the 4th row and 5th column by using normal assignment:
A[4,5] := 17.34;
For more on manipulating Matrices, Vectors, and Arrays, see the rtable_indexing Help page
Note that Matrix is different from matrix, the latter of which is now deprecated.