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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:05:23+00:00 2026-06-06T23:05:23+00:00

Suppose a 3-D matrix : >> a = rand(3,4,2) a(:,:,1) = 0.1067 0.7749 0.0844

  • 0

Suppose a 3-D matrix :

>> a = rand(3,4,2)

a(:,:,1) =

    0.1067    0.7749    0.0844    0.8001
    0.9619    0.8173    0.3998    0.4314
    0.0046    0.8687    0.2599    0.9106


a(:,:,2) =

    0.1818    0.1361    0.5499    0.6221
    0.2638    0.8693    0.1450    0.3510
    0.1455    0.5797    0.8530    0.5132

I use linear indexing to have many element at a time:

>> index1 = [1 ; 2 ; 1 ; 3];
>> index2 = [1 ; 4 ; 2 ; 3];
>> index3 = [1 ; 1 ; 2 ; 1];

>> indices = sub2ind(size(a), index1, index2, index3)

>> a(indices)

ans =

    0.1067
    0.4314
    0.1361
    0.2599

I would like to do the same thing, put return all the values of the first dimensions. The size of this dimension may vary. The return should be, in that case:

>> indices = sub2ind(size(a), ??????, index2, index3);

>> a(indices)

ans =

    0.1067    0.9619    0.0046    % a(:,1,1)
    0.8001    0.4314    0.9106    % a(:,4,1)
    0.1361    0.8693    0.5797    % a(:,2,2)
    0.0844    0.3998    0.2599    % a(:,3,1)

Any way to do that in MatLab?

  • 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-06T23:05:25+00:00Added an answer on June 6, 2026 at 11:05 pm
    ind1 = repmat((1:size(a,1)),length(index2),1);
    ind2 = repmat(index2,1,size(a,1));
    ind3 = repmat(index3,1,size(a,1));
    
    indices = sub2ind(size(a),ind1,ind2,ind3)
    
    indices =
    
     1     2     3
    10    11    12
    16    17    18
     7     8     9
    
    a(indices)
    
    
    ans =
    
        0.1067    0.9619    0.0046
        0.8001    0.4314    0.9106
        0.1361    0.8693    0.5797
        0.0844    0.3998    0.2599
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

suppose I have the following matrix a = 2 NaN NaN 4 NaN 3
Suppose I have a matrix of weights, and another matrix of data values. Can
Suppose I have a matrix foo as follows: foo <- cbind(c(1,2,3), c(15,16,17)) > foo
Suppose you have an NxN matrix and you have to check whether it's a
Suppose that I have a 2D array (matrix) in Java like this... int[][] MyMat
Suppose, in MATLAB, that I have a matrix, A, whose elements are either 0
Suppose I have D, an X-by-Y-by-Z data matrix. I also have M, an X-by-Y
Suppose I have the following matrix: 01 02 03 06 03 05 07 02
Suppose I have a rather large class Matrix , and I've overloaded operator== to
Suppose I have an AxBxC matrix X and a BxD matrix Y . Is

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.