matlab confusion matrix - matlab

I have a small problem in coding confusion 3x3 matrix by using matlab...
I tried the code below,
average = sum(diag(Mconf)./sum(Mconf,2))/3;
However, it get NaN for the confusion matrix is [0 0 0;1 2 3;4 5 6] or [1 2 3;0 0 0;4 5 6] or [1 2 3;4 5 6;0 0 0]

Try
average = sum(diag(Mconf)./sum(Mconf+eps,2))/3;
To eliminate the division by zero.

Related

First define a polynonial(As a function), and then substitude with a matrix and have some problem in product

I want to define a polynomial such as $f(x)=x^4+2x^3-4$, and want to substitude $x$ with the matrix $A$ into the polynomial. But I find the $A^4$ just work like $A.^4$ and not the correct $A^4$. How could I find some way to substitude and get a correct result?
Thanks for the comment, my code is as follows:
A=[2 0 3 1;4 5 3 3;1 0 0 1;2 3 4 5];
syms x;
f=x^4+2*x^2-4;
polyval(f,A)
I think you want Matrix polynomial evaluation. So you should polyvalm(). not polyval().
How to use it.
A=[2 0 3 1;4 5 3 3;1 0 0 1;2 3 4 5];
syms x;
f=[1 0 2 0 -4] % x^4+2*x^2-4;
polyvalm(f,A)

How can I remove NaN values from a dataset? [duplicate]

This question already has answers here:
Is there any general way to remove NaNs from a matrix?
(5 answers)
Closed 6 years ago.
New to MATLAB, any help with this would be appreciated.
I have a dataset that is 1000 elements in 1 column, and most of the elements are numbers but some are NaN's. Is there a way I can, 1. Find them, and 2. Remove them and put them in a variable (or just remove them)?
Would I have to do this the reverse way and find and remove the non-NaN's (the numbers) and store them in a variable?
Use logical indexing to extract the elements that are not NaN and then store them anywhere you like. Here's how it works. If x is your column vector containing NaN, y = ~isnan(x) will give a logical vector y such that |y|=|x| and y(i) is 1 iff x(1) is not NaN. You can use this logical vector to extract non NaN elements:
x = [1 2 3 NaN 5 6 NaN NaN 9];
y = ~isnan(x); % now y is [1 1 1 0 1 1 0 0 1]
x = x(y) % now x is [1 2 3 5 6 9]
Logical indexing is powerful and efficient. You could also say:
x = [1 2 3 NaN 5 6 NaN NaN 9];
x(isnan(x)) = -1 % now x is [1 2 3 -1 5 6 -1 -1 9]
For more information on logical indexing see the official Matlab documentation here.
A option equivalent to #sadeghmir's answer:
x = [1 2 3 NaN 5 6 NaN NaN 9];
x(x==NaN)=[];
>x
1 2 3 5 6 9

In matlab, how to generate 1:n in a matrix form without using loop ? e.g. when n=6, I want [1 2;3 4;5 6] instead of [1 2 3 4 5 6]

In MATLAB, how to generate numbers 1:n in a 2x(n/2) matrix form without using loop ? e.g.when n=6, I want [1 2;3 4;5 6] instead of [1 2 3 4 5 6].
You need to use reshape function:
n = 10;
reshape(1:n,2,[])'
ans =
1 2
3 4
5 6
7 8
9 10

Getting row and column numbers of valid elements in a matrix

I have a 3x3 matrix, populated with NaN and values of a variable:
NaN 7 NaN
5 NaN 0
NaN NaN 4
matrix = [NaN 7 NaN; 5 NaN 0; NaN NaN 4]
I would like to get the row and column numbers of non-NaN cells and put them in a matrix together with the value of the variable. That is, I would like to obtain the following matrix:
row col value
1 2 7
2 1 5
2 3 0
3 3 4
want = [1 2 7; 2 1 5; 2 3 0; 3 3 4]
Any help would be highly appreciated.
This can be done without loops:
[jj, ii, kk] = find((~isnan(matrix).*(reshape(1:numel(matrix), size(matrix)))).');
result = [ii jj matrix(kk)];
The trick is to multiply ~isnan(matrix) by a matrix of indices so that the third output of find gives the linear index of non-NaN entries. The transpose is needed to have the same order as in the question.
The following should work!
[p,q]=find(~isnan(matrix)) % Loops through matrix to find indices
want = zeros(numel(p),3) % three columns you need with same number of rows as p
for i=1:numel(p)
want[i,:] = [p(i) q(i) matrix(p(i), matrix(i))]
end
Should give you the correct result which is:
2 1 5
1 2 7
2 3 0
3 3 4
If you don't mind the ordering of the rows, you can use a simplified version of Luis Mendo's answer:
[row, col] = find(~isnan(matrix));
result = [row(:), col(:), matrix(~isnan(matrix))];
Which will result in:
2 1 5
1 2 7
2 3 0
3 3 4

write result as matrix form

let us consider following matrix
a=[1 2 3;2 3 4;3 4 5;4 5 7]
a =
1 2 3
2 3 4
3 4 5
4 5 7
let us consider it's svd
[U E V]=svd(a)
U =
-0.2738 -0.8708 -0.0062 -0.4082
-0.3984 -0.2552 -0.3309 0.8165
-0.5230 0.3605 -0.6557 -0.4082
-0.7020 0.2159 0.6786 0.0000
E =
13.5093 0 0
0 0.6482 0
0 0 0.2797
0 0 0
V =
-0.4032 0.8699 0.2841
-0.5437 0.0220 -0.8390
-0.7361 -0.4928 0.4641
if consider kronecker product of columns of U and V matrix
kron(U(:,1),V(:,1))
ans =
0.1104
0.1489
0.2015
0.1606
0.2166
0.2932
0.2109
0.2843
0.3849
0.2831
0.3817
0.5167
but it is returning as vector form,but i need matrix form,so how can i transform it to matrix insider kron product?maybe i should you reshape command,but could you help me to do it?thanks in advance
i have consider following change as Divakar adviced and it works fine
X=kron(U(:,1),V(:,1)')
X =
0.1104 0.1489 0.2015
0.1606 0.2166 0.2932
0.2109 0.2843 0.3849
0.2831 0.3817 0.5167
thanks my friend for your help