Subtracting two 3D patches/volumes from each other in Matlab - matlab

In my work, I have a nonuniform volume needs to be subtracted from my master volume/domain, and both volumes are discretized into particles, then a 3D visualization has to be constructed after the subtraction.
For a minimal example, if I have two cubes with vertices sets coordinates of
v1=[0 0 0;3 0 0;3 3 0;0 3 0;0 0 3;3 0 3;3 3 3;0 3 3]; % first cube
v2=[1 1 1;2 1 1;2 2 1;1 2 1;1 1 3;2 1 3;2 2 3;1 2 3]; % second cube
where v=[X,Y,Z] is the vertices 3D coordinates.
How can I subtract v2 from v1 and visualize the result?
P.S. I managed to create two patches as follows:
f1=convhulln(v1);
p1=patch('Vertices',v1,'Faces',f1);
f2=convhulln(v2);
p2=patch('Vertices',v2,'Faces',f2);
However, I couldn't figure out how to subtract them from each other.

Related

About decomposition of singular values and singular systems

Consider the example from the book Robust control and filtering of singular systems (Please see attached image example). I'm trying to reproduce the same example using Matlab. The problem is when I'm using the svd function I got the matrices N and M which does not satisfy the property
MAN equals to the diagonal of A and one. So to specify the soloution among all the possible SVD decompositions.
Thank you.
Example in the book:
A=[-10 5 6.5
2 -5.5 -1.25
-9 4 8.5];
E=[1 1 0.5
-0.5 1.5 1.75
1 1 0.5]; M=[2 -1 1
1 0.5 -1.5
2 -1 0];
N=[0 1 1
-1 1 1.5
-1 1 -2].
Here is my Matlab code which permits to find matrices M and N such that M[I 0 0;0 I 0;0 0 0]N=E but it does not guarantee the second statement "inv(M)Ainv(N)=[A_bar 0;0 I]"
n=3;
[M,I2,N] = svd(E);
E1=rref([I2 inv(M)]);
I2= E1(:,1:n); % the reduced row echelon form of c
M= inv(E1(:,n+1:end)); % now we have T
N=N';

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)

Matlab transpose matrix to invert plot

I plot something using as basis two matrices built with meshgrid.
[U,V] = meshgrid(Y,X);
Inside a function I build another pair of matrices
[A,B] = function(input)
and therefore I plot
plot((U((length(U)+1)/2,:)),A((length(U)+1)/2,:));
plot((V((length(U)+1)/2,:)),B((length(U)+1)/2,:));
If U and V are of this kind:
U= 1 2 3 4 V= 1 1 1 1
1 2 3 4 2 2 2 2
1 2 3 4 3 3 3 3
... ...
I want to modify A in order to have the same plot but with U transposed meaning like this
U= 1 1 1 1 V= 1 2 3 4
2 2 2 2 1 2 3 4
3 3 3 3 1 2 3 4
... ...
Means that now U has fixed values along the rows and changes along the columns, I want to have fixed values along the columns and change along the rows and the mathematical way to do it is to transpose U.
Is there another way to do it or how can I modify A to get the same plot? Of course transposing A doesn't work. A is a built like the sum of four input parameters (input of the function)
A has let's say random values but the important thing is that the center row and column are approximately zero like this
A= -1.7 -1.6 ... 0 ... 1.6 1.7
-1.6 -1.5 ... 0 ... 1.5 1.6
... 0
0 0 0 0 0
...
1.6 1.5 ... 0 ... -1.5 -1.6
1.7 1.6 ... 0 ... -1.6 -1.7
U and V are of this kind
To get the same plot after transposing U and V is this way,
U=U'; V=V';
plot((V((length(U)+1)/2,:)),A((length(U)+1)/2,:));
plot((U((length(U)+1)/2,:)),B((length(U)+1)/2,:));
but I cannot use it because afterwards I write the values of A in a file.
It is still unclear what you want.
Your matrices U and V are meshgrids.
U= 1 2 3 4 V= 1 1 1 1
1 2 3 4 2 2 2 2
1 2 3 4 3 3 3 3
And you have matrices A and B which have a correspondence to this matrices U and V.
Example:
A= -1.7 -1.6 -1.5 -1.4 B= -2.7 -2.6 -2.5 -2.4
-1.6 -1.5 -1.4 -1.3 -2.6 -2.5 -2.4 -2.3
-1.5 -1.4 -1.3 -1.2 -2.5 -2.4 -2.3 -2.2
Now you want to transpose U and V:
U= 1 1 1 1 V= 1 2 3 4
2 2 2 2 1 2 3 4
3 3 3 3 1 2 3 4
So now if we pick one location in the matrix. eg (1, 3) that will have a U value associated (1) a V value (3), a A value (-1.5) and a B value (-2.5)
If you transpose U and V, the location (1,3) will have values U,V,A,B = (3, 1, -1.5, -2.5).
As you can see, the values of A and B are mapped to different values of U and V (basically because you transposed U and V).
So transposing A and B you will have the correct mapping again.
EDIT:
You are basically plotting V in the Xaxis and A in the Yaxis.
That basically means that each element of A is associated to an element of V to form a 2D coordinate.
If you now transpose V, the X elements will be arranged differently, so you have to rearrange the elements of A so the mapping stays the same.
That means that you need to do the same operation to both matrices to keep the mapping unchanged, since you are transposing V you also need to transpose A.
I asked for a numeric example, you the one provided is unclear.
Provide an example of what you currently have (matrices + plot) and what you want to achieve (matrices and plot). Just do the example with a small 3x3 matrix and we will see what you want to do.

Color discrimination of matrix connected components

In Matlab, I have a matrix M, say:
M=[0 0 2 2 0 0
0 0 2 2 0 3
1 1 2 2 3 3
1 1 0 0 0 0
1 1 0 0 0 0];
with some connected components labeled 1,2 and 3.
I need to discriminate the components (1, 2 and 3) by using different colors (red, green and blue for example). Any help to do this. Thanks in advance
You can use image and colormap. From the documentation of the former,
image(C) displays the data in array C as an image. Each element of C
specifies the color for 1 pixel of the image.
When C is a 2-dimensional m-by-n matrix, the elements of C are used as
indices into the current colormap to determine the color. For 'direct' CDataMapping (the default),
values in C are treated as colormap indices (1-based if double, 0-based
if uint8 or uint16).
Thererfore, you only need to call image(M+1), so that the values start at 1; and then define a suitable colormap. The colormap is a 3-column matrix, where each row defines a color in terms of its R, G, B components.
M = [0 0 2 2 0 0;0 0 2 2 0 3;1 1 2 2 3 3;1 1 0 0 0 0;1 1 0 0 0 0];
imagesc(M+1) % add 1 so that values start at 1, not 0
cmap = [1 1 1; % white
.7 0 0; % dark red
0 .7 0; % dark green
0 0 .7]; % dark blue
colormap(cmap) % set colormap
axis tight % avoid white space around the values
axis equal % aspect ratio 1:1

matlab confusion matrix

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.