Multidimensional array to (multidimensional) table.

31 views (last 30 days)
I have a 3D array from a Tiff image file. The 3 dimensions are (x,y,[R, G, D]), 500x500x3. The RGDs are unsigned integers. I would love to store more property data of different types (i.e double) at the x,y positions. It seems a 3D table could accomplish this, but it does not seem MATLAB has this functionality. Am I best served to have a bunch of 500x500xN arrays for each property/data type? Should I convert the 3D array (x,y,[R, G, D]) to a table with columns x, y, R, G, D, N, M ... It seems there are some operations that are easier to perform in a table than to multiple arrays, such as searching for values in one column and then performing operations on other columns.
If converting to a table seems like a good option, is there a fancy matrix/array method for doing this, instead of the only way I could figure out using embedded for loops like the following. RGB is the original 500x500x3 array. Sorry I do not know how to use this editor.
tableline=0;
tiftable = zeros(size(RGB,1)*size(RGB,2),3); %this will be the new 250000x3 array of RGBs
for r = 1:size(RGB,1) % for all pixel rows
for c = 1:size(RGB,2) % for all pixel columns
tableline = tableline+1;
xpixel(tableline) = c;
ypixel(tableline) = r;
tiftable(tableline,:)=RGB(r,c,:);
end
end
graintable = array2table([xpixel,ypixel,tiftable],'VariableNames',{'Xpixel','Ypixel','R_orig','G_orig','B_orig'});

Answers (1)

Rajani Mishra
Rajani Mishra on 26 May 2020
array2table() function is used to covert a matrix to table, but it does not accepts 3-D matrix (image in this case) as an input arguement. So you can convert data (example image bands - R,G,B or any other property data) to a matrix where columns have required data and then use array2table for transforming to a table
Refer here for documentation of arra2table function : https://www.mathworks.com/help/matlab/ref/array2table.html
  1 Comment
Rodney McCabe
Rodney McCabe on 26 May 2020
Seems like it might be slighly more efficient, though it does not add the x and y positions(column and row) to the table. I think it would take at least as many steps to get the same table as above.
tableR = array2table(RGB(:,:,1),'VariableNames',{'R_orig'});
tableG = array2table(RGB(:,:,2),'VariableNames',{'G_orig'});
tableB = array2table(RBG(:,:,3),'VariableNames',{'B_orig'});
graintable = join(tableR, tableG, tableB];

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by