# How to concatenate Nx3 matrix vertically?

2 views (last 30 days)
OzzWal on 20 Jan 2019
Commented: OzzWal on 20 Jan 2019
I get the desired output from my code (gradient descent solver function) but I am unsure of how to concatenate each set of coordinates vertically into an Nx3 matrix?
I've tried to use "vertcat" but it still just prints the matrix horizontally in the command window ("Output truncated. Text exceeds maximum line length for Command Window display")
The desired output is a Nx3 matrix with each row containing the [xi,yi,zi] coordinates of the point interpolated at each stage of the algorithm.
%Function to implement the 2-Dimensional Gradient Descent method. The
%output of this function should be an [N x 3] matrix containing, on each
%row, the x, y and z co-ordinates of every position considered in the
%Gradient Descent algorithm (including start and end points)
function [xi, yi, zi] = gradient_descent2(Z,X0,Y0,xgrid,ygrid,gamma,tau)%Declare function
%[xgrid, ygrid] = meshgrid(x,y); %make mesh grid
%---------------------------------2--------------------------------------%
%Use cubic interpolation to obtain starting point z(X0,Y0) and gradient at
%this point
Z0 = interp2(xgrid,ygrid,Z,X0,Y0,'cubic');
%---------------------------------3--------------------------------------%
%Declare xi,yi,zi before while loop
xi = X0 - gamma*gradZ0_x; %GD algorithm:
zi = interp2(xgrid,ygrid,Z,xi,yi,'cubic');
xi(1) = X0;%add starting point coordinates
yi(1) = Y0;
zi(1) = Z0;
j=1;%index counter
threshold = tau+1;%meet threshold condition to enter while loop
coordsOut = [];%Declare empty matrix to store coords
coordsOut(j,:) = [xi, yi, zi];
while tau <=threshold
j = j+1;%update j
xi(j) = xi(j-1) - gamma*gradZi_x; %update x,y coords
zi(j) = interp2(xgrid,ygrid,Z,xi(j-1),yi(j-1),'cubic');%update z with cubic interpolation from new x,y coords
coordsOut=vertcat([coordsOut; [xi(j), yi(j), zi(j)]]);%add coordinates to matrix
threshold = sqrt((xi(j)-xi(j-1))^2 + (yi(j)-yi(j-1))^2);%threshold condition
end
g=sprintf('%d ', coordsOut);%print coordinates
end

Guillaume on 20 Jan 2019
but it still just prints the matrix horizontally in the command window
Yes. because that's exactly how you're asking matlab to display it. With
fprintf('%d ', array_of_any_shape)
matlab just replicates the format string until the end of the array. So it's equivalent to:
fprintf('%d %d %d %d ... as many as necessary', array_of_any_shape(:))
The simplest way to display your array according to its shape is to use disp, so:
disp(coordsOut)
and you're done. If you really want to use fprintf or sprintf, then you need to use:
fprintf('%d %d %d\n', coordsOut.'); %the array needs to be transposed as well
Finally, note that in
coordsOut=vertcat([coordsOut; [xi(j), yi(j), zi(j)]]);
the vertcat does nothing (you only pass it one argument, so it's got nothing to concatenate) and the second sets of [] is also useless.
coordsOut=[coordsOut; xi(j), yi(j), zi(j)];

#### 1 Comment

OzzWal on 20 Jan 2019
Thank you - yes, you're right. I must have just pulled that from somewhere without truly understanding it.