Thread: 2D to 3D matrix like matlab reshape function.

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    2

    2D to 3D matrix like matlab reshape function.

    I am converting matlab code to C. Here I am stuck in the matlab reshape function. I am not quite getting the idea to implement in C.
    So far I have written like this


    Code:
    int*** copy2dTo3D(int rows, int cols,int levels, int colsize,int **ar)
    {
    int i,j,k,x,y;
    int*** array_3d;
    
    
    array_3d = (int***)malloc(levels*sizeof(int **));
    for (i = 0; i < levels; i++)
    {
    array_3d[i]=(int **)malloc(rows*sizeof(int *));
    for (j = 0; j < rows; j++)
    {
    array_3d[i][j]=(int *)malloc(cols*sizeof(int));
    for (k = 0; k < cols; k++)
    {
    
    
                     if(j%2==0)
                     {
                     array_3d[i][j][k] = ar[j][x++];
                     printf("%d ",array_3d[i][j][k]);
                         if(x==colsize) x=0;
                        }
                        else
                        {
                         array_3d[i][j][k] = ar[j][y++];
                         printf("%d ",array_3d[i][j][k]);
                         if(y==colsize) y=0;
                        }
    }
    }
    }
    return array_3d;
    }
    but this is getting me segmentation fault. In matlab if you provide a 2D matrix and mention the size(row*col*levels) of new 3D matrix. It does. And while it does it makes in each level a row*col size matrix.
    Most important thing while filling the new row*col matrix it takes data from 2D matrix like this:

    twoD =


    1 2 3 4 5 6
    7 8 9 10 11 12
    three3D = reshape(twoD, 2,2,3)
    and result is like
    three3D(:,:,1) =


    1 2
    7 8




    three3D(:,:,2) =


    3 4
    9 10




    three3D(:,:,3) =


    5 6
    11 12



    any idea how to implement it in C? I am banging my head into wall literally for days.
    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. Your indentation is awful.
    2. You don't initialise x and y.
    3. You don't need the malloc casts if you included stdlib.h and are using a C compiler. If you're using a C++ compiler, then stop it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    2
    Hi Salem,
    1.while copying to forum I was changing the code and put the x,y uninitialised. Sorry for that. it main code its x=0,y=0.
    2. Same goes for the
    indentation.
    3. According my knowledge this is how I dynamically allocate memory for array. New suggestion would be nice.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well I guess the first thing is post your REAL code, not some random paraphrasing from memory.

    Your approach isn't unreasonable as it is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  2. Problem with matrix function.
    By ariella in forum C Programming
    Replies: 10
    Last Post: 07-14-2013, 06:09 AM
  3. Matlab into C Function
    By a.mlw.walker in forum C Programming
    Replies: 6
    Last Post: 08-12-2011, 01:43 PM
  4. help Passing matrix to a function
    By rob90 in forum C Programming
    Replies: 3
    Last Post: 11-19-2009, 06:48 AM
  5. Array equivalent to MatLab's 'find' function
    By magda3227 in forum C Programming
    Replies: 2
    Last Post: 06-16-2008, 02:54 PM

Tags for this Thread