How to plot all the matrix elements in matlab whitout knowing the size? - matlab

Lets say there is the next matrix
A = [ 1 2 2 ;
1 2 3 ;
2 3 4 ;
3 4 5 ;
4 4 6 ;
1 11 12]
I try to use quiver3 to plot the row in the next way:
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);
and so on until the last row, but how can apply quiver3 for each row of the matrix instead of making one line per row?
Besides, isn't the same size of matrix always, so making one one command per row would yield some rows without plotting sometimes and perhaps not enough rows to plot other .
(Example: the matrix provided has 6 rows so I make 6 quiver3 expressions, but later if the matrix has only 3 rows, it gives me an error and if later it has 8 rows, there would be 2 vectors/row that aren't plotted).
Im guessing that it has to do with the range and meshgrid operator but I can not see how.

If your matrix A has N rows, where each row stores the [u v w] components to pass to quiver3, then you can plot all N arrows in one call to quiver3, provided you ensure all of your input arguments are the same size. If you are plotting all of them starting from the origin, then you have to create an N-by-1 vector of zeroes to use for your x, y, and z inputs:
A = [1 2 2;
1 2 3;
2 3 4;
3 4 5;
4 4 6;
1 11 12];
z = zeros(size(A, 1), 1);
quiver3(z, z, z, A(:, 1), A(:, 2), A(:, 3), 0);
Note the indexing syntax I used to split A up into columns to pass to quiver3. And here's the resulting plot for the given sample data:

Related

Using splines() forms problem if x coordinates go from increasing to decreasing

I have two columns with data, x and y. Now I want to connect these data points in the order they appear in the columns. Say I have x=[1 2 3 4 3 2] and y=[3 4 2 1 3 3]. Now if I use spline to create a smooth curve, it 'sorts' the columns in an increasing order. I would like it to just take the data points, thus firstly x(1),y(1) and connect these to x(2), y(2) and so on.
Is this possible?
spline generates a function from the reals to the reals. This means a more general curve cannot be expressed as y = f(x) but we need to parametrize it as (x(t), y(t)):
x=[1 2 3 4 3 2];
y=[3 4 2 1 3 3];
plot(x,y,'o-');
% cannot be represented as function y=f(x)
% because x=2 and 3 have two different y values
% -> parametrize x and y:
t = 1:numel(x);
tt = linspace(min(t), max(t), 1000);;
tx = spline(t,x,tt);
ty = spline(t,y,tt);
hold on
plot(tx,ty,'-');

MATLAB localmax() function returns indexes in 1D vector

In order to calculate local maximums of 2D matrix Y, I use this
[~, indices]= localmax(Y);
But the indices is 1D. How to convert it back to 2D so to access its corresponding element in Y?
From the documentation for localmax:
Linear indices of the nonzero values of lmaxima. Use ind2sub to
convert the linear indices to matrix row and column indices.
For example:
inputmatrix = ...
[3 2 5 3
4 6 3 2
4 4 7 4
4 6 2 2];
[~,indices] = localmax(inputmatrix,4,false);
[I, J] = ind2sub(size(indices), indices);
Edit: I should have clarified as well. As #LuisMendo mentions in the comments above, you can access the elements of Y directly with these linear indices by using Y(indices).

Count occurrences and stretch array in matlab

Let
input = [0 0 0 5 5 7 8 8];
I now want to transform this vector into the form
output = [3 3 3 3 5 5 6 8];
Which basically is a stairs plot.
Explanation
The input vector is used to plot data points along the x-axis. The y-axis is thereby provided by 1:length(input). So the resulting plot shows the cumulative number of datapoints along the y-axis and the time of occurrence along the x-axis.
I now want to fit a model against my dataset. Therefor I need a vector that provides the correct value for a certain time (x-value).
The desired output vector basically is the result of a stairs plot. I am looking for an efficient way to generate the desired vector in matlab. The result of
[x, y] = stairs(input, 1:length(input));
did not bring me any closer.
It can be done with bsfxun as follows:
x = [0 0 0 5 5 7 8 8];
y = sum(bsxfun(#le, x(:), min(x):max(x)), 1);
This counts, for each element in 1:numel(x), how many elements of x are less than or equal to that.

How do I add up two scatter points with the same values of x but different values of y?

On a stem plot, how can I add points that have the same values of x but different values of y?
For example, given the following code:
x = [1 2 3 6 6 4 5];
y = [3 6 1 8 9 4 2];
stem(x,y);
If you plot x, and y, this will be the output:
I want to add up (6,8) and (6,9) so it becomes (6,17), just like what the image is showing.
How can I achieve this?
Use accumarray with x and y so you can bin or group like entries together that share the same x. Once these values are binned, you can sum all of the values that share the same bin together. As such, we see that for x = 6, we have y = 8 and y = 9. accumarray allows you to group multiple y values together that share the same x. Once these values are grouped, you then apply a function to all of the values in the same group to produce a final output for each group. In our case, we want to sum them, so we need to use the sum function:
x = [1 2 3 6 6 4 5];
y = [3 6 1 8 9 4 2];
Z = accumarray(x(:), y(:), [], #sum);
stem(unique(x), Z);
xlim([0 7]);
We use unique on X so that we have no repeats for X when plotting the stem plot. unique also has the behaviour of sorting your x values. Doing x(:) and y(:) is so that you can make your input data either as row or column vectors independently. accumarray accepts only column vectors (or matrices, but we won't go there) and so doing x(:) and y(:) ensures that both inputs are column vectors.
We get:
The above code assumes that x is integer and starting at 1. If it isn't, then use the third output of unique to assign each number a unique ID, then run this through accumarray. When you're done, use the output of accumarray like normal:
[xu,~,id] = unique(x);
Z = accumarray(id, y(:), [], #sum);
stem(xu, Z);

Matlab: Solve Exponential Equation with two unknown parameters

I have a Matrix called A. For example the following:
A = [1 2 3; 3 4 1; 2 4 4]
Now I have the following equation:
A(x,y) = (j^x)*(i^y)
j and i are normal values (dimension 1x1), not indices of a matrix. ^
Lets make an example:
A(1,1) = 1 (First value of the Matrix)
1 = (j^1)*(i^1)
And a second one:
A(1,2) = 3
3 = (j^1)*(i^2)
Is there a possibility to receive one solution for the two parameters using Matlab?
Here is some code that can find the best solution to your problem, if there is one. In this case, there is no reasonable solution, but defining A by M([4 2]) (for example) does work reasonably well.
A = [1 2 3; 3 4 1; 2 4 4] %// the A matrix
[C,R]=meshgrid(1:3) %// create matrices of row/column indices
M=#(xy) xy(2).^C.*xy(1).^R %// calculates matrix of elements j^x*i^y
d=#(xy) A-M(xy) %// calculates difference between A and the calculated i^x*y^j matrix
r=fsolve(#(xy) norm(d(xy)),[1 1]) %// use fsolve to attempt to find a solution
d(r) %// show resulting difference between target matrix and solution matrix
norm(d(r)) %// norm of that matrix
M(r) %// show the solution matrix