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 7801415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:56:40+00:00 2026-06-02T00:56:40+00:00

I have a non-fixed dimensional matrix M, from which I want to access a

  • 0

I have a non-fixed dimensional matrix M, from which I want to access a single element.
The element’s indices are contained in a vector J.

So for example:

M = rand(6,4,8,2);
J = [5 2 7 1];

output = M(5,2,7,1)

This time M has 4 dimensions, but this is not known in advance. This is dependent on the setup of the algorithm I’m writing. It could likewise be that

M = rand(6,4);
J = [3 1];

output = M(3,1)

so I can’t simply use

output=M(J(1),J(2))

I was thinking of using sub2ind, but this also needs its variables comma separated..

@gnovice

this works, but I intend to use this kind of element extraction from the matrix M quite a lot. So if I have to create a temporary variable cellJ every time I access M, wouldn’t this tremendously slow down the computation??

I could also write a separate function

function x= getM(M,J)
    x=M(J(1),J(2));
    % M doesn't change in this function, so no mem copy needed = passed by reference
end

and adapt this for different configurations of the algorithm. This is of course a speed vs flexibility consideration which I hadn’t included in my question..

BUT: this is only available for getting the element, for setting there is no other way than actually using the indices (and preferably the linear index). I still think sub2ind is an option. The final result I had intended was something like:

function idx = getLinearIdx(J, size_M)
    idx = ...
end

RESULTS:

function lin_idx = Lidx_ml( J, M )%#eml
%LIDX_ML converts an array of indices J for a multidimensional array M to
%linear indices, directly useable on M
%
% INPUT
%   J       NxP matrix containing P sets of N indices
%   M       A example matrix, with same size as on which the indices in J
%           will be applicable.
%
% OUTPUT
%   lin_idx Px1 array of linear indices
%

% method 1
%lin_idx = zeros(size(J,2),1);
%for ii = 1:size(J,2)
%    cellJ = num2cell(J(:,ii)); 
%    lin_idx(ii) = sub2ind(size(M),cellJ{:}); 
%end

% method 2
sizeM = size(M);
J(2:end,:) = J(2:end,:)-1;
lin_idx = cumprod([1 sizeM(1:end-1)])*J;

end

method 2 is 20 (small number of index sets (=P) to convert) to 80 (large number of index sets (=P)) times faster than method 1. easy choice

  • 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-06-02T00:56:43+00:00Added an answer on June 2, 2026 at 12:56 am

    For the general case where J can be any length (which I assume always matches the number of dimensions in M), there are a couple options you have:

    1. You can place each entry of J in a cell of a cell array using the num2cell function, then create a comma-separated list from this cell array using the colon operator:

      cellJ = num2cell(J);
      output = M(cellJ{:});
      
    2. You can sidestep the sub2ind function and compute the linear index yourself with a little bit of math:

      sizeM = size(M);
      index = cumprod([1 sizeM(1:end-1)]) * (J(:) - [0; ones(numel(J)-1, 1)]);
      output = M(index);
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have vector of strings and want to create a fixed with string out
I have a non-rails application that I want to use rails active-record migrations with.
I have several strings which look like the following: <some_text> TAG[<some_text>@11.22.33.44] <some_text> I want
I work a lot with files which contain data on fixed positions. Non-delimited CSV
When you have non-fixed width characters (such as \t ) in a string ,
I have non-ascii chars in velocity template files. And when processed they are garbled.
I have non-template class with a templatized constructor. This code compiles for me. But
I have a non databound DGV (no datasource, etc; rows added manually). To filter
I have an (non-virtualized) ItemsControl that binds its ItemsSource to a ObeservableCollection of ViewModel
I have some non well-formed xml (HTML) data in JAVA, I used JAXP Dom,

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.