How do I get the index of a matrix that is stored in a 4D matrix?












0















I am writing some code, whereby I store a greyscale image, which is split into 'blocks' in a 4D array. I will be looping through all the 'blocks' in the 4D array and will perform calculations based on the contents of the blocks compared to one another. I want to only compare the 'blocks' that are near each other, and to do this I can just calculate the distance between the 'blocks' and don't loop through the ones that are too far away. To do this I need the index of each 'block' in the 4D matrix, ultimately creating my question.



My code goes like this:



for i=4dmatrix1
for j=4dmatrix2
% Do calculations here involving the index of i
% and j in their respective matrices.
end
end


I have i and j, but I want to find their index in 4dmatrix1 and 4d matrix2 respectively. 4dmatrix1 and 4dmatrix2 are greyscaled images that have been split into "blocks" of 20x20 pixels. Each matrix in 4dmatrix1 and 4dmatrix2 is a "block" in image 1 and image 2. The reason I have used this method for storing the data as it still represents the shape of the image, just split into 20x20 blocks. In my head this is understandable, but maybe for programming, this is inefficient and should be changed. If so, what would you recommend looking into?



Thank you!










share|improve this question























  • For indexing questions I can recommend this Q/A which lists all the different kinds of indexing.

    – Adriaan
    Nov 13 '18 at 21:38











  • This indexing Q/A doesn't specify anything about multidimensional arrays, only 2D arrays.

    – S_Zizzle
    Nov 13 '18 at 21:55
















0















I am writing some code, whereby I store a greyscale image, which is split into 'blocks' in a 4D array. I will be looping through all the 'blocks' in the 4D array and will perform calculations based on the contents of the blocks compared to one another. I want to only compare the 'blocks' that are near each other, and to do this I can just calculate the distance between the 'blocks' and don't loop through the ones that are too far away. To do this I need the index of each 'block' in the 4D matrix, ultimately creating my question.



My code goes like this:



for i=4dmatrix1
for j=4dmatrix2
% Do calculations here involving the index of i
% and j in their respective matrices.
end
end


I have i and j, but I want to find their index in 4dmatrix1 and 4d matrix2 respectively. 4dmatrix1 and 4dmatrix2 are greyscaled images that have been split into "blocks" of 20x20 pixels. Each matrix in 4dmatrix1 and 4dmatrix2 is a "block" in image 1 and image 2. The reason I have used this method for storing the data as it still represents the shape of the image, just split into 20x20 blocks. In my head this is understandable, but maybe for programming, this is inefficient and should be changed. If so, what would you recommend looking into?



Thank you!










share|improve this question























  • For indexing questions I can recommend this Q/A which lists all the different kinds of indexing.

    – Adriaan
    Nov 13 '18 at 21:38











  • This indexing Q/A doesn't specify anything about multidimensional arrays, only 2D arrays.

    – S_Zizzle
    Nov 13 '18 at 21:55














0












0








0








I am writing some code, whereby I store a greyscale image, which is split into 'blocks' in a 4D array. I will be looping through all the 'blocks' in the 4D array and will perform calculations based on the contents of the blocks compared to one another. I want to only compare the 'blocks' that are near each other, and to do this I can just calculate the distance between the 'blocks' and don't loop through the ones that are too far away. To do this I need the index of each 'block' in the 4D matrix, ultimately creating my question.



My code goes like this:



for i=4dmatrix1
for j=4dmatrix2
% Do calculations here involving the index of i
% and j in their respective matrices.
end
end


I have i and j, but I want to find their index in 4dmatrix1 and 4d matrix2 respectively. 4dmatrix1 and 4dmatrix2 are greyscaled images that have been split into "blocks" of 20x20 pixels. Each matrix in 4dmatrix1 and 4dmatrix2 is a "block" in image 1 and image 2. The reason I have used this method for storing the data as it still represents the shape of the image, just split into 20x20 blocks. In my head this is understandable, but maybe for programming, this is inefficient and should be changed. If so, what would you recommend looking into?



Thank you!










share|improve this question














I am writing some code, whereby I store a greyscale image, which is split into 'blocks' in a 4D array. I will be looping through all the 'blocks' in the 4D array and will perform calculations based on the contents of the blocks compared to one another. I want to only compare the 'blocks' that are near each other, and to do this I can just calculate the distance between the 'blocks' and don't loop through the ones that are too far away. To do this I need the index of each 'block' in the 4D matrix, ultimately creating my question.



My code goes like this:



for i=4dmatrix1
for j=4dmatrix2
% Do calculations here involving the index of i
% and j in their respective matrices.
end
end


I have i and j, but I want to find their index in 4dmatrix1 and 4d matrix2 respectively. 4dmatrix1 and 4dmatrix2 are greyscaled images that have been split into "blocks" of 20x20 pixels. Each matrix in 4dmatrix1 and 4dmatrix2 is a "block" in image 1 and image 2. The reason I have used this method for storing the data as it still represents the shape of the image, just split into 20x20 blocks. In my head this is understandable, but maybe for programming, this is inefficient and should be changed. If so, what would you recommend looking into?



Thank you!







matlab matrix multidimensional-array indexing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 21:07









S_ZizzleS_Zizzle

85




85













  • For indexing questions I can recommend this Q/A which lists all the different kinds of indexing.

    – Adriaan
    Nov 13 '18 at 21:38











  • This indexing Q/A doesn't specify anything about multidimensional arrays, only 2D arrays.

    – S_Zizzle
    Nov 13 '18 at 21:55



















  • For indexing questions I can recommend this Q/A which lists all the different kinds of indexing.

    – Adriaan
    Nov 13 '18 at 21:38











  • This indexing Q/A doesn't specify anything about multidimensional arrays, only 2D arrays.

    – S_Zizzle
    Nov 13 '18 at 21:55

















For indexing questions I can recommend this Q/A which lists all the different kinds of indexing.

– Adriaan
Nov 13 '18 at 21:38





For indexing questions I can recommend this Q/A which lists all the different kinds of indexing.

– Adriaan
Nov 13 '18 at 21:38













This indexing Q/A doesn't specify anything about multidimensional arrays, only 2D arrays.

– S_Zizzle
Nov 13 '18 at 21:55





This indexing Q/A doesn't specify anything about multidimensional arrays, only 2D arrays.

– S_Zizzle
Nov 13 '18 at 21:55












1 Answer
1






active

oldest

votes


















0














You can loop over the indices of a matrix in any dimension, and then map that to the subscripts using ind2sub. Basically, the syntax would be



[id1,id2,id3,id4] = ind2sub(size(my4Dmatrix, i));


And something similar for j.

Not really your question, but something doesn't seem quite right with how you're looping. Also, you should include a minimum working example, including generating a couple of matrices and using correct syntax (you cannot start a variable name in MATLAB with a number).






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289501%2fhow-do-i-get-the-index-of-a-matrix-that-is-stored-in-a-4d-matrix%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can loop over the indices of a matrix in any dimension, and then map that to the subscripts using ind2sub. Basically, the syntax would be



    [id1,id2,id3,id4] = ind2sub(size(my4Dmatrix, i));


    And something similar for j.

    Not really your question, but something doesn't seem quite right with how you're looping. Also, you should include a minimum working example, including generating a couple of matrices and using correct syntax (you cannot start a variable name in MATLAB with a number).






    share|improve this answer




























      0














      You can loop over the indices of a matrix in any dimension, and then map that to the subscripts using ind2sub. Basically, the syntax would be



      [id1,id2,id3,id4] = ind2sub(size(my4Dmatrix, i));


      And something similar for j.

      Not really your question, but something doesn't seem quite right with how you're looping. Also, you should include a minimum working example, including generating a couple of matrices and using correct syntax (you cannot start a variable name in MATLAB with a number).






      share|improve this answer


























        0












        0








        0







        You can loop over the indices of a matrix in any dimension, and then map that to the subscripts using ind2sub. Basically, the syntax would be



        [id1,id2,id3,id4] = ind2sub(size(my4Dmatrix, i));


        And something similar for j.

        Not really your question, but something doesn't seem quite right with how you're looping. Also, you should include a minimum working example, including generating a couple of matrices and using correct syntax (you cannot start a variable name in MATLAB with a number).






        share|improve this answer













        You can loop over the indices of a matrix in any dimension, and then map that to the subscripts using ind2sub. Basically, the syntax would be



        [id1,id2,id3,id4] = ind2sub(size(my4Dmatrix, i));


        And something similar for j.

        Not really your question, but something doesn't seem quite right with how you're looping. Also, you should include a minimum working example, including generating a couple of matrices and using correct syntax (you cannot start a variable name in MATLAB with a number).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 21:39









        Steve HeimSteve Heim

        686922




        686922






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289501%2fhow-do-i-get-the-index-of-a-matrix-that-is-stored-in-a-4d-matrix%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python