How to plot several vectors qith quiver3 in matlab - matlab

Im trying to plot the elements of a given matrix
A = [ 1 2 3; 3 4 5, 5 6 7];
using quiver3, so all of these are in the same figure using:
figure(3);
hold on;
quiver3(0,0,0,A(1,1),A(1,2),A(1,3),0);
quiver3(0,0,0,A(2,1),A(2,2),A(2,3),0);
quiver3(0,0,0,A(3,1),A(3,2),A(3,3),0);
with the next result
instead of something like this
How can tell to quiver3 that plots all the vectors at the same time, or is there anyother command that can do this?

Related

convert a line plot to a matrix

I connect several points two by two by
plot([x1,x2],[y1,y2]) %create line
% x1,y1 = coordinates of point 1
% x2,y2 = coordinates of point 2
and I would like to know if it's possible to keep this different links into a matrix to display them subsequently with imagesc or imshow (this matrix will also be useful to me outside the display later)
As long as I understand, you are trying to plot multiple "lines" in one plot without using hold.
You can use concatenation for this purpose as shown below:
plot([1 ;2 ],[1;2] );
xlim([1 6]);
ylim([1 6]);
plot([3 ;4 ],[3;4] )
xlim([1 6]);
ylim([1 6]);
plot([5 ;6 ],[5;6] );
xlim([1 6]);
ylim([1 6]);
Finally if you want to plot these all lines into one plot you can use concatenation in the second dimension.
plot([1 3 5 ; 2 4 6 ],[1 3 5 ; 2 4 6] );
In order to save it and make it again plot table you can store in matrix with another dimension
matrixTest(:,:,1) = [1 3 5 ; 2 4 6 ];
matrixTest(:,:,2) = [1 3 5 ; 2 4 6 ];
plot(matrixTest(:,:,1),matrixTest(:,:,2));

Plot 3D histogram using bar3

I'm currently trying to plot the output of hist3 using bar3. This is a simple example:
vec_x = [1 2 4 5 7 8 9 3 8 7 2]';
vec_y = [1 3 9 5 7 8 1 3 2 9 2]';
vec_bin_edges = 0:9;
hist3([vec_x vec_y], 'Edges', {vec_bin_edges, vec_bin_edges});
mat_joint = hist3([vec_x vec_y], 'Edges', {vec_bin_edges, vec_bin_edges});
figure
bar3(mat_joint, 1);
axis tight
In order to demonstrate my issue, I made two pics of both figures:
This one is the output of hist3([vec_x vec_y], 'Edges', {vec_bin_edges, vec_bin_edges});
This one is the output of bar3(mat_joint, 1);
As you can see, the bar3 function does not really "bin" the data values as hist3 does, so the bars are shifted slightly in their positions. My question is now, whether it's possible to make the bar3 plot look exactly like the hist3 plot. My motivation to do so is, that I need to modify the mat_joint matrix and plot it again, which is not possible using hist3.
EDIT: The different colors are not important, it's just about the bin positions
ok, I figured it out:
set(gca, 'xtick', [1.5:1:10.5]);
set(gca, 'ytick', [1.5:1:10.5]);
vec_bin_labels = 1:10;
vec_string_bin_labels = reshape(cellstr(num2str(vec_bin_labels(:))), size(vec_bin_labels));
set(gca, 'xticklabel', vec_string_bin_labels);
set(gca, 'yticklabel', vec_string_bin_labels);

Adding a curve to Matlab Figure

I have written a program that takes a lot of data and produces graphs.It would be really convenient and save me a lot of time if I could take curves on an existing figure and add their values together to make a single curve. For a simple example lets say I have the following code,
x = [0 1 2 3 4 5];
y = [0 1 2 3 4 5];
z = [4 6 2 8 7 9];
figure
plot(x,y,x,z)
This code will produce a figure with two curves. Without modifying the code or re-running the program, and only working with the figure options I would like to add the curve y + z to the plot. Is this possible? Thanks.
The reason I don't want to add the functionality is the plot code is buried within 8 loops
that calls data from a 4D cell array of file name strings.
If you have the x, y and z variable used in the plot you can just add new lines to the plot with
hold on
plot(x,y+z)
hold off
If you don't have them directly (they were generated in a function, for example, you can always get them from figure with XData, YData properties of line objects.
hline = findobj(gca,'type','line');
x = get(hline,'XData');
y = get(hline,'YData');
X = x{1}; % let's assume that all lines have the same x values.
Y = sum(cell2mat(y));
hold on
plot(X,Y)
hold off

How can I plot multiple 3D arrays on a single plot?

I have multiple arrays of 3D data. How can I plot them all on a single plot? The size of the arrays are not equal.
For example:
array1_xy = [1 2;3 4;5 6]
array1_z = [10;20;30]
array2_xy = [2 4;5 6;4 6;4 5]
array2_z = [10;20;50;10]
array3_xy = [1 4;1 6;1 3;1 5;1 1;3 4]
array3_z = [10;20;30;10;80;30]
How can I plot them on a single 3D plot with different markers?
You want the hold function.
From the link above:
x = -pi:pi/20:pi;
plot(sin(x))
hold on
plot(cos(x))
hold off
This will plot sin(x) and on the same axes it'll plot cos(x).
If you want to plot your arrays with plot3 function, you can still use hold on; and plot them in the same graph.
I assume you want to do a xyz scatter plot(?) in that case, use plot3. Details go help plot3
Details about markers, go help plot
Follow code does what you want.
plot3(array1_xy(:,1),array1_xy(:,2),array1_z,'x'); hold on;
plot3(array2_xy(:,1),array2_xy(:,2),array2_z,'o');
plot3(array3_xy(:,1),array3_xy(:,2),array3_z,'p');

Need help in plotting lines between points

I need help in plotting lines between points.
Suppose, I start with creating 6 random points-
x = rand(6,1);
y = rand(6,1);
So my points are (x(1),y(1)), (x(2),y(2)), (x(3),y(3)), (x(4),y(4)), (x(5),y(5)), (x(6),y(6))
Now I want to draw straight lines between the points 1 & 5, 2 & 6, 3 & 4
and plot them in a single diagram. So I get 3 straight lines.
Any help would be highly appreciated.
You can do this with one call to PLOT. If you reshape your x and y data into matrices with each column containing a set of coordinates for one line, then PLOT will draw a different colored line for each column:
index = [1 2 3; 5 6 4]; %# The index to reshape x and y into 2-by-3 matrices
plot(x(index),y(index)); %# Plot the lines
Here are two ways to do this:
First way, using hold on. These lines are separate, i.e if you turn one red, the others will stay blue.
%# plot the first line
plot([x(1);x(5)],[y(1);y(5)]);
hold on %# this will prevent the previous plot from disappearing
%# plot the rest
plot([x(2);x(6)],[y(2);y(6)]);
plot([x(3);x(4)],[y(3);y(4)]);
Second way, making use of the fact that NaN does not get plotted. These lines are grouped, i.e. if you turn one red, all will be red.
%# create array for plotting
xy = NaN(8,2);
%# fill in data
xy([1 2 4 5 7 8],1) = x([1 5 2 6 3 4]);
xy([1 2 4 5 7 8],2) = y([1 5 2 6 3 4]);
%# plot
plot(xy(:,1),xy(:,2))