What I would like to do is visualise the change in 3 dimensional data. For example i have two arrays:
before:
x y z
1 2 3
4 5 6
7 8 9
after:
x y z
2 2 3
5 5 6
8 8 9
I would like for the plot to be 3D scatter data like the folowing:
I know about quiver3 which plots norms but i am not sure how to do it from just 2 lists of X, Y, Z points.
The actual data will be much more complex.
Thanks for your help.
Arrow.m is available from the Matlab File Exchange that makes drawing arrows really easy:
>> A = [1 2 3; 4 5 6; 7 8 9];
>> B = [2 2 3; 5 5 6; 8 8 9];
>> hold on
>> scatter3(A(:,1), A(:,2), A(:,3))
>> scatter3(B(:,1), B(:,2), B(:,3))
>> arrow(A, B)
Otherwise, take a look at other answers to this question.
Related
I have a matrix as follows:
A= 1 2
3 4
5 6
7 8
I want to arrange the elements of this matrix in such a way that it will give me the following output:
B= 1
2
3
4
5
6
7
8
Any kind of suggestion will be helpful. Thanks!
Take the transpose of A and unroll it into a vector:
B = A.';
B = B(:);
Alternatively, you can use reshape:
B = reshape(A.', [], 1);
The reason why you transpose the matrix A first is because MATLAB does the unrolling in column-major format, which means that the columns are traversed first. You are trying to do this row-wise, and so you'd need to transpose the input to achieve the same effect.
Here's what the output looks like (using both):
>> A= [1 2
3 4
5 6
7 8];
>> B = A.';
>> B = B(:);
>> B
B =
1
2
3
4
5
6
7
8
Also:
>> A= [1 2
3 4
5 6
7 8];
>> B = reshape(A.', [], 1)
B =
1
2
3
4
5
6
7
8
I want to draw a color map with three columns in matlab.
I can draw with plot3 like below,
x = [1 1 1 1 2 2 2 2 4 4 4 4 5 5 5 5 9 9 9 9];
y = [2 3 4 5 5 6 7 8 4 5 6 7 1 2 3 4 7 8 9 10];
z = [1 3 2 4 5 6 7 3 9 8 8 9 2 4 3 5 1 2 3 1];
plot3(x, y, z, 'o')
But how can I draw 2D color map with three columns?
Option 1:
If I understand you correctly you want to draw a 2D array (say m(x,y)) where the color is given by z. this is how:
m=zeros(max(x),max(y)); % preallocate m according to values of x,y
m(sub2ind(size(m),x,y))=z; % assign z-values to the x,y coordinates
imagesc(m) % plot
colormap(pink(max(z))); % set colormap with the dynamic range of z.
% you can replace it with jet or whatever...
colorbar % add a colorbar
Option 2:
you really just want to create am RGB colormap from x,y,z:
cmap=[x(:) y(:) z(:)]./max([x(:);y(:);z(:)]);
imagesc(peaks(100));
colormap(cmap);
This question already has answers here:
Got confused with a vector indexed by a matrix, in Matlab
(2 answers)
Closed 8 years ago.
Suppose:
a =
1 2 3
4 5 6
2 3 4
and
b =
1 3 2
6 4 8
In MATLABa(b) gives:
>> a(b)
ans =
1 2 4
3 2 6
What is the reason for this output?
when you have a matrix a:
a =
1 2 3
4 5 6
7 8 9
and b:
b =
1 3 4
3 2 6
then a(b) is a way of adressing items in a and gives you:
>> a(b)
ans =
1 7 2
7 4 8
to understand this you have to think of a als a single column vector
>> a(:)
ans =
1
4
7
2
5
8
3
6
9
now the first row of b (1 3 4) addresses elements in this vector so the first, the 3rd and the forth element of that single column vector which are 1 7 and 2 are adressed. Next the secound row of b is used as adresses for a secound line in the output so the 3rd, the 2nd and the 6th elements are taken from a, those are 7 4 and 8.
It's just a kind of matrix indexing.
Matrix indexes numeration in 'a' matrix is:
1 4 7
2 5 8
3 6 9
This is a possible duplicate to this post where I gave an answer: Got confused with a vector indexed by a matrix, in Matlab
However, I would like to duplicate my answer here as I think it is informative.
That's a very standard MATLAB operation that you're doing. When you have a vector or a matrix, you can provide another vector or matrix in order to access specific values. Accessing values in MATLAB is not just limited to single indices (i.e. A(1), A(2) and so on).
For example, let's say we had a vector a = [1 2 3 4]. Let's also say we had b as a matrix such that it was b = [1 2 3; 1 2 3; 1 2 3]. By doing a(b) to access the vector, what you are essentially doing is a lookup. The output is basically the same size as b, and you are creating a matrix where there are 3 rows, and each element accesses the first, second and third element. Not only can you do this for a vector, but you can do this for a matrix as well.
Bear in mind that when you're doing this for a matrix, you access the elements in column major format. For example, supposing we had this matrix:
A = [1 2
3 4
5 6
7 8]
A(1) would be 1, A(2) would be 3, A(3) would be 5 and so on. You would start with the first column, and increasing indices will traverse down the first column. Once you hit the 5th index, it skips over to the next column. So A(5) would be 2, A(6) would be 4 and so on.
Here are some examples to further your understanding. Let's define a matrix A such that:
A = [5 1 3
7 8 0
4 6 2]
Here is some MATLAB code to strengthen your understanding for this kind of indexing:
A = [5 1 3; 7 8 0; 4 6 2]; % 3 x 3 matrix
B = [1 2 3 4];
C = A(B); % C should give [5 7 4 1]
D = [5 6 7; 1 2 3; 4 5 6];
E = A(D); % E should give [8 6 3; 5 7 4; 1 8 6]
F = [9 8; 7 6; 1 2];
G = A(F); % G should give [2 0; 3 6; 5 7]
As such, the output when you access elements this way is whatever the size of the vector or matrix that you specify as the argument.
In order to be complete, let's do this for a vector:
V = [-1 9 7 3 0 5]; % A 6 x 1 vector
B = [1 2 3 4];
C = V(B); % C should give [-1 9 7 3]
D = [1 3 5 2];
E = V(D); % E should give [-1 7 0 9]
F = [1 2; 4 5; 6 3];
G = V(F); % G should give [-1 9; 3 0; 5 7]
NB: You have to make sure that you are not providing indexes that would make the accessing out of bounds. For example if you tried to specify the index of 5 in your example, it would give you an error. Also, if you tried anything bigger than 9 in my example, it would also give you an error. There are 9 elements in that 3 x 3 matrix, so specifying a column major index of anything bigger than 9 will give you an out of bounds error.
This is the input matrix
7 9 6
8 7 9
7 6 7
Based on the frequency their appearance in the matrix (Note. these values are for explanation purpose. I didn't pre-calculate them in advance. That why I ask this question)
number frequency
6 2
7 4
8 1
9 2
and the output I expect is
4 2 2
1 4 2
4 2 4
Is there a simple way to do this?
Here's a three-line solution. First prepare the input:
X = [7 9 6;8 7 9;7 6 7];
Now do:
[a m n] = unique(X);
b = hist(X(:),a);
c = reshape(b(n),size(X));
Which gives this value for c:
4 2 2
1 4 2
4 2 4
If you also wanted the frequency matrix, you can get it with this code:
[a b']
Here is a code with for-loop (a is input matrix, freq - frequency matrix with 2 columns):
weight = zeros(size(a));
for k = 1:size(freq,1)
weight(a==freq(k,1)) = freq(k,2);
end
Maybe it can be solved without loops, but my code looks like:
M = [7 9 6 ;
8 7 9 ;
7 6 7 ;];
number = unique(M(:));
frequency = hist(M(:), number)';
map = containers.Map(number, frequency);
[height width] = size(M);
result = zeros(height, width); %allocate place
for i=1:height
for j=1:width
result(i,j) = map(M(i,j));
end
end
I have two matlab vectors. The first has N elements, the other has k*N. I know what k is, and I want to splice the lists such that each element from the first vector appears before the corresponding k elements from the next vector. For example:
k = 3
x = [1 5 9]
y = [2 3 4 6 7 8 10 11 12]
should be combined to look like this:
z = [1 2 3 4 5 6 7 8 9 10 11 12]
Is there an easy way to do this quickly? My x's and y's are pretty big. Thanks!
You can do this via some reshaping
k = 3
x = [1 5 9]
y = [2 3 4 6 7 8 10 11 12]
%# make a k-by-n array
z = reshape(y,k,[]);
%# catenate with x
z = [x;z];
%# reorder
z = z(:)'