Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6244591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:21:09+00:00 2026-05-24T12:21:09+00:00

I am trying to implement a simple matlab-like array (now only one dimension actually),

  • 0

I am trying to implement a simple matlab-like array (now only one dimension actually), what I tried to do is to implement the following matlab codes:

a=1:10;
ind=find(a>5);
a[ind]=5;

I know that the std has valarray to do this through a slice array. I do not know much details on it. The code is:

#include <iostream>
using namespace std;
template <typename T> class array
{
public:
    int m,n; //two dimensional at most
    T *pdata;
    //construct the array
    array(){m=n=0;pdata=NULL;} //default is empty matrix
    array(T a){m=n=1;pdata=new T[1];*pdata=a;} //array for scalar: array a=10;
    array(int m0,int n0=1) {m=m0;n=1;pdata=new T[m];}
    array(const array& a,int len=-1);
    //destructor
    ~array() {delete []pdata;}
    //operator overloading
    array<T>& operator+=(T s);
    T& operator[](int i) {return pdata[i];}
    array<T>& operator[](array<int> ind);
    array<T>& operator=(const array<T>& a);
    array<T>& operator=(T a) {for(int i=0;i<m;i++) pdata[i]=a;return *this;}
    array<bool> operator>(T a);
    array<bool> operator<(T a);
    array<bool> operator==(T a);
};

//copy a part of the other array
template <typename T> array<T>::array<T>(const array<T>& a,int len)
{
    if(len==-1) len=a.m*a.n;
    if(len==0) {m=0;n=0;pdata=NULL;}
    if(len>0)
    {
        m=len;n=1;
        pdata=new T[len];
        for(int i=0;i<len;i++) pdata[i]=a.pdata[i];
    }
}

template <typename T> array<T>& array<T>::operator +=(T s)
{
    for(int i=0;i<m*n;i++) pdata[i]+=s;
    return *this;
}

//this function does not meet the purpose, it returns a reference to a temp obj
template <typename T> array<T>& array<T>::operator[](array<int> ind)
{
    array<T> ret(ind.m,ind.n);
    for(int i=0;i<ind.m*ind.n;i++)
    {
        ret.pdata[i] = pdata[ind.pdata[i]];
    }
    return ret;
}

template <typename T> array<bool> array<T>::operator>(T a)
{
    array<bool> res(m*n);
    for(int i=0;i<m*n;i++) res.pdata[i]=pdata[i]>a;
    return res;
}

//helper function
array<int> find(array<bool> a)
{
    array<int> ret(a.m,a.n); //first use the same size space
    int len=0;
    for(int i=0;i<a.m*a.n;i++)
    {
        if(a.pdata[i]) {ret.pdata[len]=i;len++;}
    }
    return array<int>(ret,len);
}

/*ostream& operator<<(array<T>& a)
{
    ostream os;
    for(int i=0;i<a.m*a.n;i++) os>>a[i]>>'\t';
    return os;
}*/

int main()
{
    array<float> a(10);
    for(int i=0;i<10;i++) a[i]=i;
    for(i=0;i<10;i++) cout<<a[i]<<'\t';
    cout<<endl;
    array<int> ind=find(a>5);
    for(i=0;i<ind.m;i++) cout<<ind[i]<<'\t';
    cout<<endl;
    a[ind]=5;//this will not work on the original array
    //how do we support this????undefined
    for(i=0;i<10;i++) cout<<a[i]<<'\t';
    cout<<endl;

    return 0;
}

The final a is not changed at all since we are working on a temp array.
I know the function operator”> is not properly implemented, but I do not know how to do this. Anyone can give me a hint? Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:21:10+00:00Added an answer on May 24, 2026 at 12:21 pm

    Andrea, Thank you for the hint. Mostly what you said makes sense and really helps. I create another ind_array and keep the original array’s address and now it works!
    The only change is the operator[] now returns the ind_array. and the operator= for ind_array is defined to do the real assignment. (to make it simple, I now removed the second dimension)

    Here is the modified code:

    #include <iostream>
    using namespace std;
    template <typename T> class ind_array;
    template <typename T> class array
    {
    public:
        int len; //two dimensional at most
        T *pdata;
        //construct the array
        array(){len=0;pdata=NULL;} //default is empty matrix
        //array(T a){len=1;pdata=new T[1];*pdata=a;} //array for scalar: array a=10;
        array(int m0) {len=m0;pdata=new T[len];}
        array(const array& a,int len0=-1);
        //destructor
        ~array() {delete []pdata;}
        int size() const {return len;}
        //operator overloading
        array<T>& operator+=(T s);
        T& operator[](int i) {return pdata[i];}
        ind_array<T> operator[](const array<int>& ind);//{return (ind_array(ind,pdata));}
        array<T>& operator=(const array<T>& a);
        array<T>& operator=(T a) {for(int i=0;i<len;i++) pdata[i]=a;return *this;}
        array<bool> operator>(T a);
        array<bool> operator<(T a);
        array<bool> operator==(T a);
    };
    
    //Index array or similar indirect-array as in valarray
    //this class shall keeps the array's address and the index
    template <typename T> class ind_array
    {
        array<int> ind; //an index array
        T* ptr; //a pointer to the original data
    public:
        int size() const {return ind.size();} 
        void operator=(T a){for(int i=0;i<size();i++) ptr[ind[i]]=a;} //assignment a value to a subarray
        //how to construct the indx array then?
        //according to valarry, the default constructor shall be prohibited
        ind_array(const array<int>& indx,T* pt):ind(indx),ptr(pt){} //default constructor
    };
    
    //copy a part of the other array
    template <typename T> array<T>::array<T>(const array<T>& a,int len0)
    {
        if(len0==-1) len0=a.len;
        if(len0==0) {len=0;pdata=NULL;}
        if(len0>0)
        {
            len=len0;
            pdata=new T[len];
            for(int i=0;i<len;i++) pdata[i]=a.pdata[i];
        }
    }
    
    template <typename T> array<T>& array<T>::operator +=(T s)
    {
        for(int i=0;i<len;i++) pdata[i]+=s;
        return *this;
    }
    
    //this function does not meet the purpose, it returns a reference to a temp obj
    //now we change it to return a indx_array which stores the original array's address
    template <typename T> ind_array<T> array<T>::operator[](const array<int>& ind)
    {
        /*array<T> ret(ind.len);
        for(int i=0;i<ind.len;i++)
        {
            ret.pdata[i] = pdata[ind.pdata[i]];
        }
        return ret;*/
        return (ind_array<T>(ind,pdata)); //call the constructor
    }
    
    template <typename T> array<bool> array<T>::operator>(T a)
    {
        array<bool> res(len);
        for(int i=0;i<len;i++) res.pdata[i]=pdata[i]>a;
        return res;
    }
    
    //helper function
    array<int> find(array<bool> a)
    {
        array<int> ret(a.len); //first use the same size space
        int len=0;
        for(int i=0;i<a.len;i++)
        {
            if(a.pdata[i]) {ret.pdata[len]=i;len++;}
        }
        return array<int>(ret,len);
    }
    
    /*ostream& operator<<(array<T>& a)
    {
        ostream os;
        for(int i=0;i<a.m*a.n;i++) os>>a[i]>>'\t';
        return os;
    }*/
    
    int main()
    {
        array<float> a(10);
        for(int i=0;i<10;i++) a[i]=i;
        for(i=0;i<10;i++) cout<<a[i]<<'\t';
        cout<<endl;
        array<int> ind=find(a>5);
        for(i=0;i<ind.len;i++) cout<<ind[i]<<'\t';
        cout<<endl;
        a[ind]=5;//this will not work on the original array
        //how do we support this????undefined
        for(i=0;i<10;i++) cout<<a[i]<<'\t';
        cout<<endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am now trying to implement a simple HTML webpage scraper using Java.Now I
I'm trying to implement a simple priority queue from array of queues. I'm trying
I'm trying to implement simple sliding window alogirithm on two-dimensional array in C# 3.0,
I'm trying to implement a simple search function. I have a string array which
I'm trying to implement simple Event Bus I started like this: public class RegistrationData
I am trying to implement a simple log using Nlog 1.0, using the following
I'm trying to implement a simple search, using LIKE in my SQL statement: Using
I am trying to implement a simple delay-sum beamformer using a 4 Microphone Array.
i'm trying to implement a simple array of function descriptors of type fun_desc struct
I'm trying to implement a simple shell program that supports multiple piping. For now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.