How to transform a 3D list to a 2D list?

I want to apply the command Histogram3D to an image.The image data is

{{r1,g1,b1},{r2,g2,b2}...}


And the input of Histogram3D should be {{x1,y1},{x2,y2}...}.

How can I transform r,g,b list into x,y list?

• This is very confusing. The three values $r, g, b$ are fundamentally three-dimensional, and a histogram describes the frequency of appearance of different values in a list. Please clarify what you seek... perhaps with a hand-crafted tiny example. – David G. Stork Sep 18 '17 at 23:04
• I think it may be just like that:the former form is{{1,2,3},{4,5,6}},and the later form is{{1,2},{3,4},{5,6}}.Could it be possible? – Luxuan Qi Sep 19 '17 at 4:07
• What sense would it make to have {{r1,g1,b1},{r2,g2,b2}...{rn,gn,bn}} and then make a histogram of {{r1,g1},{b1,r2},{g2,b2}...{b(n-1), rn},{gn,bn}}  ? Any case, look at this ArrayReshape[ Array[Through[{r, g, b}[#]] &, 12] , {6, 2}]. – rhermans Sep 19 '17 at 10:37
• I provided an answer to an identical question here – rhermans Sep 19 '17 at 14:28
• @rhermans Thank you very much!I'm taking an image processing course and it is one of questions in an assignment.I spend long time on this question but can not work it out.And I see your answer in the identical question and it really helps me a lot. – Luxuan Qi Sep 19 '17 at 23:18

You can turn a list of 3-tuples into a list of 2-tuples by using Part (which has shortcut [[ ]])

list = {{r1, g1, b1}, {r2, g2, b2}, {r3, g3, b3}, {r4, g4, b4}};
list[[All, 1 ;; 2]]

{{r1, g1}, {r2, g2}, {r3, g3}, {r4, g4}}


or by using ReplaceAll and an appropriate pattern

list /. {x_, y_, z_} -> {x, y}


In[88]:= list1 = {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}};
l = Dimensions[list1][[1]]*Dimensions[list1][[2]]
list2 = Flatten[list1]
list3 = Table[{list2[[2 i - 1]], list2[[2 i]]}, {i, 1, Round[l/2]}]
If[Mod[l, 2] == 1, r = list2[[l]]]


gives

Out[89]= 9
Out[90]= {x1, y1, z1, x2, y2, z2, x3, y3, z3}
Out[91]= {{x1, y1}, {z1, x2}, {y2, z2}, {x3, y3}}
Out[92]= z3


Thus, the solution depends on the parity of the length of the dimensions of your initial list: you can have a remainder.

In a comment you suggest that your desired result could be, from the list {{1,2,3},{4,5,6}}, to get {{1,2},{3,4},{5,6}}. I'm not sure what data structure this will actually be useful for, but that result is very easily achieved as

{{1,2,3},{4,5,6}}//Catenate//Partition[#,2]&


and playing around with the options for Partition will let you take care of different sizes of input.