By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Reg: Accessing elements of 3d matrix

100+
P: 105
Hi all,

Actually, I have read a data file of an image from its sub header stored the data in a buffer.*imdata is a pointer to the buffer for data. The data size is 100x100x81 and no. of bytes per pixel is 4 bytes and the format type is float. Now I have to read all the rowa and columns of 40th row in 3rd dimension. My problem is how to access it through this pointer imdata. Thanks in advance.

imsize_x=100;
imsize_y=100;
imsize_z=81;
nobytesperpixel=4;
I want to get all the elements 100x100 of z=40.
Mar 12 '07 #1
Share this Question
Share on Google+
11 Replies


100+
P: 1,646
I am a little confused by your question.
Is it that you have the value z=40 and you want to access another set of data that relates to this value?
Mar 12 '07 #2

100+
P: 105
Hi willakawill,

Sorry if I anot clear.I am just giving you an example of what I meant.

For eg: a[3][3][2]

a[ ][ ][1]={2,3,4; 1,2,3;4,5,6};

a[ ][ ][2]={1,1,1;2,2,2;3,3,3};

Now I would like to access all the rows and columns of a[ ][ ][2];

Here I have pointer to this buffer of data as I have mentioned before *imdata.So now I want to access these elemnts for eg: a[ ][ ] [40] using imdata.
Mar 12 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
Using two for loops, iterate through the first two dimensions of the matrix, using the indexes and 40 when reading data. Something like this:
Expand|Select|Wrap|Line Numbers
  1. FOR (Index i, from 0 to X) {
  2.    FOR (Index j, from 0 to Y) {
  3.       READ imdata[i][j][40];
  4.       // DO STUFF...
  5.    }
  6. }
where X and Y are the dimensions.
Mar 12 '07 #4

100+
P: 105
Hi,

I have declared the pointer as float *imgdata;

I just tried to check to display one value.
But when I say just cout<<imgdata[1][1][1] or cout<<*imgdata[1][1][1] it says

error C2109: subscript requires array or pointer type.

In case if I say imgdata[1000] it displays some value.

Could you please help me how I need to declare this pointer ?Am I doing something wrong?

Thank you very much for all your help.
Mar 12 '07 #5

Expert 100+
P: 1,510
you could define imgadata so
Expand|Select|Wrap|Line Numbers
  1.     float ***imgdata;
  2.  
create the storage etc, then access the elements
Expand|Select|Wrap|Line Numbers
  1. cout<<imgdata[1][1][1];
  2.  
Mar 12 '07 #6

DeMan
100+
P: 1,806
I think (and pointers confuse me, especially when substituted with arrays) that if you want to have three indexes you need to warn the compiler that this is what you can do.....

That is, you can declare:
float [][][] imgdata; OR
float ***imgdata
BUT when you declare float *imgdata, the compiler doesn't understand why you add three indexes ("imgdata[1][1][1]")
Mar 12 '07 #7

100+
P: 105
Hi all,

Thank you all for the replies and your help.

Declaring the pointer can solve but actually my read function takes a pointer to single array like read(const *imgdata, imgsize).It says it cannot convert from float* to float***.I am thinking the read function takes the parameter as a pointer to 1D array and I cannot change this function as it is written by someone.

I have created the memory as
imgdata=new float[imgsize]; ///Thinking something wrong here

where imgsize is calculated as multiplication of all the dimensions = 1000(if data is 10x10x10).


Do you think the way I created memory is wrong?But I am thinking still my read function still cannot convert.

Could you give me any suggestions or any other options???Thanks in advance.
Mar 12 '07 #8

Ganon11
Expert 2.5K+
P: 3,652
If you have to use a read function written by someone else, but you have to read/process the data as a 3D array, then I'd say you have a problem O.O

Not sure how to tackle this one...
Mar 12 '07 #9

DeMan
100+
P: 1,806
I think declaring as float[1000], means that the data is considered as 1000 floats....
while using float[10][10][10] would still give 1000 floats, it is actually 10 arrays of 10 arrays of 10 floats (which I think is what you want)......

you could create your own way to access the data within your array of 1000 floats, but I'm not sure this is a particularly good idea, as it complicates the code (which introduces scope for extra errors).
Mar 13 '07 #10

100+
P: 105
Deman,

Thank you for all the options you have suggested.Actually I am trying to access it as single array.Thanks once again.
Mar 13 '07 #11

DeMan
100+
P: 1,806
I think then you could cretae your own accessor methods to give the appearance of a 3d array
eg:
you have X elements in single array, and want to treat the array as a 3d array where a, b, c are the lengths of respective arrays (and therefore abc = X) then:
You need to be consistent with which value you use where, but basically any item in the array (can be accessed for some values i,j,k (all less than their a,b,c counterparts respecively) then the value in the array could be calculated as i*(b*c) + j*c + k (You can use other combinations to form this sum, but you need to be careful that the value you compare with a (ie i<=a) isn't multioplied by a.

This probably sounds confusing, but if we substitute 10 in for a,b,c so that X is 1000, then for i, j, k you would store at 100i + 10j + k, which is unique for any i, j, k less than a==b==c==10.,....
Mar 13 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.