function [ m, n, ncc, icc, ccc, acc ] = crs_to_ccs ( m, n, nz, row, col, val )

%*****************************************************************************80
%
%% crs_to_ccs() converts a CRS matrix to CCS format.
%
%  Discussion:
%
%    A sparse mxn matrix with nz nonzeros can be set in compressed 
%    row storage (CRS) format.
%
%    A sparse mxn matrix with ncc nonzeros can be set in compressed 
%    column storage (CCS) format.
%
%    This function converts from CRS to CCS format.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    11 June 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer M, N: the number of rows and columns.
%
%    integer NZ: the number of nonzero elements.
%
%    integer ROW(M+1): the nonzero offdiagonal elements 
%    of row I of A are contained in A(ROW(I)) through A(ROW(I+1)-1).
%
%    integer COL(NZ): the column indices of the nonzero elements of A.
%
%    real VAL(NZ): the nonzero elements of A.
%
%  Output:
%
%    integer M, the number of rows.
%
%    integer N, the number of columns.
%
%    integer NCC, the number of values.
%
%    integer ICC(NCC), the rows.
%
%    integer CCC(N+1), the compressed columns
%
%    real ACC(NCC), the values.
%

%
%  Allocate space.
%
  column_length = zeros ( n, 1 );

  ncc = nz;
  icc = zeros ( ncc, 1 );
  ccc = zeros ( n + 1, 1 );
  acc = zeros ( ncc, 1 );
%
%  Compute the number of nonzeros in each column.
%
  for k = 1 : nz
    j = col(k);
    column_length(j) = column_length(j) + 1;
  end
%
%  Construct the column pointer array.
%
  ccc(1) = 1;
  for j = 1 : n
    ccc(j+1) = ccc(j) + column_length(j);
  end
%
%  Record the first open position in each column.
%
  column_free = zeros ( n, 1 );
%
%  For each matrix entry, determine its column j, and place
%  the row i and value v in the next free spot for that column.
%
  for i = 1 : m
    for c = row(i) : row(i+1) - 1
      v = val(c);
      j = col(c);
      k = ccc(j) + column_free(j);
      acc(k) = v;
      icc(k) = i;
      column_free(j) = column_free(j) + 1;
    end
  end

  return
end

