Hello everyone,

I have c++ function whose the only argument is an array. Also I have a matrix. How can I pass say third column of this matrix as a array into the function without creating a mediated array in the loop.

Is there c++ analogue of M(:,3) existing in MATLAB and Fortran?

Thank you

Assuming your matrix is a two dimensional array, and the function takes a one dimensional array of the same type, you can do this:

some_function(my_matrix[3]);

I'm assuming a lot because you didn't provide nearly enough information to properly answer the question.

Thanks. I think that's what I am looking for. One more question. What is about passing a line of the matrix (not column)? In Fortran, I can take M(N,: ) in this case.

If you want both, you're SOL. Some kind of copying has to be done to end up with the right type.

OK Thank you. I think many people are SOL in this case :) Should looking for a good matrix class library for c++. However, good news are about matrix columns.

Maybe you'd simply be better off using MATLAB or Fortran. :icon_rolleyes: While it's possible to shoehorn behavior from other languages into C++ with specialized libraries, the results are often (if not always) sub-optimal. Your best bet with C++ is writing code to work with the language rather than fight it.

Unfortunately or fortunately, c++ language have another features which are not contained in MATLAB or Fortran. An example is popen function for Linux. However, I think c++ is not most universal programming language. More universal one is math. Other languages try to realize matrix data manipulation in correspondence with matrix theory. c++ is a little bit odd in this regard. )

>>Other languages try to realize matrix data manipulation in correspondence with matrix theory. c++ is a little bit odd in this regard.

Well, C++ does not try to realize matrix data manipulation at all. So, it is not surprising if it seems odd to you. Languages like Fortran and Matlab are two examples of languages which were built with the explicit intent of realizing matrix operations naturally (Fortran did not succeed much in that regard, although it is used a lot and has many legacy and well-optimized code for numerical analysis in general). But, you pay for everything in programming. Matlab is very nice, natural and optimized for doing matrix operations, but it quickly gets awkward when you move away from matrix numerical analysis. C++ is probably the most universal programming language, which also means that it has to please a whole lot of people doing many different things. And not everyone is doing matrix operations, or numerical analysis, or anything related to math for that matter. If you try to provide nice built-in features to please everyone, you end-up pleasing no-one (e.g. Java). The approach in C++ is to provide only the most incontestably useful functionality in its standard libraries and then provide unparalleled language features to allow the development of awesome libraries for special purposes.

And so, to the point, there are plenty of nice open-source libraries for matrix operations. They range from very easy-to-use (often resembling Matlab) to extremely optimized and robust (often old cryptic libraries like LAPACK, or not as easy-to-use libraries based on OpenCL or OpenMP which are highly optimized and run in parallel). There is plenty of choice for this in C++. But, I guess for your purpose, that could be overkill.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.