File Exchange

image thumbnail

Find the "I J K" indices of any element number "e" in the 2D or 3D matrix

version 1.2 (1.84 KB) by

Many a times it may be required to identify the indices, 'i','j' and 'k' values of an element number

4 Ratings



View License

Many a times it may be required to identify the indices, 'i'(row),'j'(column) and 'k'(plane) values of an element number 'e'

% [row,column,plane]=findijk(79,[10 10 10])
% row =
% 9
% column =
% 8
% plane =
% 1
% [row,column,plane]=findijk(79,[10 10])
% row =
% 9
% column =
% 8
% plane =
% 1
% [row,column,plane]=findijk(301,[10 10 10])
% row =
% 1
% column =
% 1
% plane =
% 4

[NOTE: Its just another method and users who are inquisitive enough to know a different approach (as with the inbuilt function ind2sub) to do the same job, would find this interesting.]

Comments and Ratings (9)

Stephen Cobeldick

Does not do anything that MATLAB's inbuilt |ind2sub| does not do:

Richard Crozier

Ok, the code is formatted a bit better now, but it would be even better if you also formatted the help a bit better too. For example, the first line after the function definition should be a one line description of what the function does. This is known as the h1 line and is displayed when you use the 'lookfor' function to search for a function. Following this should be your help text. There should be no break in the text so that is is all displayed when you type 'help findijk' at the command prompt. You should also move all the comments at the end of the file to the help text at the top instead so it is also displayed in this case. Finally, at the bottom of your help you should add the line:

% See also IND2SUB, SUB2IND

To make users aware of these functions.

I have updated the file accordingly, in terms of the formatting and layout:). I agree the inbuilt function is better. Users inquisitive enough to know about another approach to the same job may find this interesting. The updated file should appear online soon...

Thank you for your comment and the rating :)

Richard Crozier

Actually I see it's not your first post, and your others seem reasonable, so I'm not sure what made you think this one was a good idea. For this reason, one star.

Richard Crozier

ok, but your code is also terrible, badly formatted, confusing, and badly laid out. If it is intended to help anyone looking on the file exchange, it is pretty much as bad as it can be for this purpose. At worst, it might have hidden the existence of ind2sub from some new user.

Perhaps consider not posting anything further until it either does a task not already available in matlab or by some other file on the file exchange, or is a good example of matlab coding that might educate someone. I refrained from giving you one star as this is clearly your first post, next time I'd be forced to be honest. Also consider removing this file, as it adds nothing to the exchange.

Sorry if I am being too blunt.

Dear RichardCrozier and John D'Errico,,, thank you for our comments and rating.

Actually, I was not aware of a function like sub2ind when i needed it. So, had to write my own code.

Since i have followed a different approach for getting the same answer, i wanted to share it with the community.

Yeah true, the inbuilt matlab code is more complete. I will think of generalizing my code too and will repost it someday. Thanks :)

John D'Errico

John D'Errico (view profile)

Why use this when the functionality already exists in matlab, and has existed for many years? And of course, ind2sub is more complete than this code is too.

Richard Crozier

Or you could use matlab built-in function ind2sub, e.g.

[row,col,plane] = ind2sub(rcp,e)


>> [row,col,plane] = ind2sub([10 10 10],79)

row =


col =


plane =




In accordance with the comment on this file, i have included adequate changes in the code as regards to the code formatting and layout. Also included a NOTE in the code description.


included a small change in the title and included an errata in the description.

MATLAB Release
MATLAB 7.11 (R2010b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today