Monthly Archives: August 2008

Using C++ iterators on MATLAB mxArrays

Here’s a little something for the MATLAB lovers out there who also program in C++. The code sample at the end of this post shows how to add a C++ iterator to an mxArray, which is the basic datatype for MATLAB arrays. This makes it harder to walk off the end of an mxArray during linear traversal of all of the elements in an array and (hopefully) improves the readability of code that accesses the data stored inside mxArrays. The code gives a couple simple-minded examples of element-wise traversal (provided the MATLAB array you give it is a UINT8 array).

If you’re familiar with the C++ Standard Library, you can start to use your wrapped mxArray like an STL container. I use the term “like” advisedly, since it’s not actually a container. To reduce memory overhead, I don’t make a copy of the data that gets put into ArrayWrapper. Consequently, it doesn’t model the container concept that element lifetimes are the same as the “container” lifetime. You’re shouldn’t count on being able to use ArrayWrapper with any ole STL function. But then again, you might be able to use it with some; I haven’t tried.

#include "mex.h" #include <cstddef> // Treat an mxArray like a container. // (See Josuttis. "The C++ Standard Library." 1999. p. 219-220) template<class T> class ArrayWrapper {   private:     T      *v;     mwSize thesize;   public:     // Type definitions     typedef T         value_type;     typedef T*        iterator;     typedef const T*  const_iterator;     typedef T&        reference;     typedef const T&  const_reference;     typedef mwSize    size_type;     typedef ptrdiff_t difference_type;     // Constructor     ArrayWrapper(const mxArray *theArray)     {         v = static_cast<T*>(mxGetData(theArray));         thesize = mxGetNumberOfElements(theArray);     }     // Iterator support     iterator begin() { return v; }     const_iterator begin() const { return v; }     iterator end() { return v + thesize; }     const_iterator end() const { return v + thesize; }     // Direct element access     reference operator[](size_type i) { return v[i]; }     const_reference operator[](size_type i) const { return v[i]; }     // Size is constant     size_type size() const { return thesize; }     size_type max_size() const { return thesize; } }; void mexFunction(int nlhs, mxArray *plhs[],                  int nrhs, const mxArray *prhs[]) {     const mxArray *inputArray = prhs[0];     ArrayWrapper<uint8_T> inData(inputArray);     // 1- Count the number of samples in the image.     mwSize count = 0;     ArrayWrapper<uint8_T>::const_iterator i = inData.begin();     while (i++ != inData.end())     {         count++;     }     mexPrintf("The image has %ld samples.\n", count);     // 2 - Halve the image values.     mxArray *outputArray = mxDuplicateArray(prhs[0]);     ArrayWrapper<uint8_T> outData(outputArray);     i = inData.begin();     ArrayWrapper<uint8_T>::iterator j = outData.begin();     while (i != inData.end())     {         *(j++) = *(i++) * 0.5;     }         plhs[0] = outputArray; }
Posted in C, Computing, MATLAB | 2 Comments

What I did on my summer vacation (part 3)

And now a word from your sponsors.

This message was brought to you by Lightroom, Perl, Aquamacs Emacs, and Cyberduck.

Posted in General, Photography, This is who we are, Travel, USA, Western Adventure | Leave a comment

What I did on my summer vacation (part 2)

“The other day, I saw a bear. A great big bear, a way out there. . .”

Driving back to East Glacier one day, we decided to count the different kinds of (larger) animals we saw on our trip: mule deer, prairie dogs, black bears, mountain goats, brook trout, magpies, ravens, goldfinch, pronghorn, elk, bison, pelicans, marmots, chipmunks (two kinds), squirrels (tree and ground), bunnies, hummingbirds, coyote, cows, camels, llamas, horses, osprey, golden eagles, robins, blue jays, geese, ducks, mosquitos, doggies, hawk, dragonflies, grizzly bears, bighorn sheep, bees, and hippies.

Here are photographs of some of these animals.

Hummingbird (Rocky Mountain NP, Colorado) Hummingbird
(Rocky Mountain NP, Colorado)
Yellow-bellied marmot (Rocky Mountain NP, Colorado) Yellow-bellied marmot
(Rocky Mountain NP, Colorado)
Mallard (Rocky Mountain NP, Colorado) Mallard
(Rocky Mountain NP, Colorado)
Yellow-bellied marmot (Rocky Mountain NP, Colorado) Yellow-bellied marmot
(Rocky Mountain NP, Colorado)
Elk (Rocky Mountain NP, Colorado) Elk
(Rocky Mountain NP, Colorado)
Elk (Rocky Mountain NP, Colorado) Elk
(Rocky Mountain NP, Colorado)
Chipmunk (Rocky Mountain NP, Colorado) Chipmunk
(Rocky Mountain NP, Colorado)
Pronghorn antelope (Natrona County, Wyoming) Pronghorn antelope
(Natrona County, Wyoming)
Ravens (Yellowstone NP, Wyoming) Ravens
(Yellowstone NP, Wyoming)
Elk (Yellowstone NP, Wyoming) Elk
(Yellowstone NP, Wyoming)
Bison (Yellowstone NP, Wyoming) Bison
(Yellowstone NP, Wyoming)
Bison (Yellowstone NP, Wyoming) Bison
(Yellowstone NP, Wyoming)
Bison (Yellowstone NP, Wyoming) Bison
(Yellowstone NP, Wyoming)
Elk (Yellowstone NP, Wyoming) Elk
(Yellowstone NP, Wyoming)
Coyote (Yellowstone NP, Wyoming) Coyote
(Yellowstone NP, Wyoming)
Deer (Yellowstone NP, Wyoming) Deer
(Yellowstone NP, Wyoming)
Black bear (Yellowstone NP, Wyoming) Black bear
(Yellowstone NP, Wyoming)
Osprey (Glacier NP, Montana) Osprey
(Glacier NP, Montana)
Cows (Glacier NP, Montana) Cows
(Glacier NP, Montana)
Deer (Waterton Lakes NP, Alberta) Deer
(Waterton Lakes NP, Alberta)
Black bear (Waterton Lakes NP, Alberta) Black bear
(Waterton Lakes NP, Alberta)
Deer (Glacier NP, Montana) Deer
(Glacier NP, Montana)
Grizzly bears (Blackfeet Nation, Montana) Grizzly bears
(Blackfeet Nation, Montana)
Bighorn sheep (Glacier NP, Montana) Bighorn sheep
(Glacier NP, Montana)
Mountain goat (Glacier NP, Montana) Mountain goat
(Glacier NP, Montana)
Mountain goats (Glacier NP, Montana) Mountain goats
(Glacier NP, Montana)
Posted in Photography, Travel, USA, Western Adventure | 1 Comment

What I did on my summer vacation (part 1?)

“Jeff, we demand pictures! You’ve been back for two weeks. How long can it take?”

Alright. Alright. But you’re only going to get a rough timeline.

Downtown Denver Downtown Denver
Coors Field, Denver Coors Field, Denver
At low elevation, ca. 7,000 feet (Rocky Mountain NP, Colorado) At low elevation, ca. 7,000 feet
(Rocky Mountain NP, Colorado)
On our first hike, ca. 8,500 feet (Rocky Mountain NP, Colorado) On our first hike, ca. 8,500 feet
(Rocky Mountain NP, Colorado)
Lunch spot (Rocky Mountain NP, Colorado) Lunch spot
(Rocky Mountain NP, Colorado)
On our second hike, ca. 9,500 feet (Rocky Mountain NP, Colorado) On our second hike, ca. 9,500 feet
(Rocky Mountain NP, Colorado)
On our second hike, ca. 10,000 feet (Rocky Mountain NP, Colorado) On our second hike, ca. 10,000 feet
(Rocky Mountain NP, Colorado)
On our second hike, Lake Haiyaha (Rocky Mountain NP, Colorado) On our second hike, Lake Haiyaha
(Rocky Mountain NP, Colorado)
Where's the parking lot? (Rocky Mountain NP, Colorado) Where’s the parking lot?
(Rocky Mountain NP, Colorado)
On Trail Ridge Road, ca. 13,000 feet (Rocky Mountain NP, Colorado) On Trail Ridge Road, ca. 13,000 feet
(Rocky Mountain NP, Colorado)
Elk on the tundra (Rocky Mountain NP, Colorado) Elk on the tundra
(Rocky Mountain NP, Colorado)
Mother bear and cubs (Rocky Mountain NP, Colorado) Mother bear and cubs
(Rocky Mountain NP, Colorado)
Arty elks (Rocky Mountain NP, Colorado) Arty elks
(Rocky Mountain NP, Colorado)
Carbon County, Wyoming Carbon County, Wyoming
Downtown Casper, Wyoming Downtown Casper, Wyoming
Learning how to use the big camera (Casper, Wyoming) Learning how to use the big camera
(Casper, Wyoming)
Freeland Cemetery (Natrona County, Wyoming) Freeland Cemetery
(Natrona County, Wyoming)
Freeland Cemetery (Natrona County, Wyoming) Freeland Cemetery
(Natrona County, Wyoming)
Freeland Cemetery (Natrona County, Wyoming) Freeland Cemetery
(Natrona County, Wyoming)
In the Boone and Crockett lodge at the Buffalo Bill Historical Center (Cody, Wyoming) In the Boone and Crockett lodge at the Buffalo Bill Historical Center
(Cody, Wyoming)
In the Whitney museum at the BBHC (Cody, Wyoming) In the Whitney museum at the BBHC
(Cody, Wyoming)
A typical prairie scene (Cody, Wyoming) A typical prairie scene
(Cody, Wyoming)
Another typical prairie scene (Cody, Wyoming) Another typical prairie scene
(Cody, Wyoming)
Yet another typical prairie scene (Cody, Wyoming) Yet another typical prairie scene
(Cody, Wyoming)
Yellowstone NP, Wyoming Yellowstone NP, Wyoming
Early summer flowers (Yellowstone NP, Wyoming) Early summer flowers
(Yellowstone NP, Wyoming)
Bison amid the flowers (Yellowstone NP, Wyoming) Bison amid the flowers
(Yellowstone NP, Wyoming)
Old Faithful (Yellowstone NP, Wyoming) Old Faithful
(Yellowstone NP, Wyoming)
Grand Prismatic Pool (Yellowstone NP, Wyoming) Grand Prismatic Pool
(Yellowstone NP, Wyoming)
Home, Sweet Home (Yellowstone NP, Wyoming) Home, Sweet Home
(Yellowstone NP, Wyoming)
On our third hike (Yellowstone NP, Wyoming) On our third hike
(Yellowstone NP, Wyoming)
On our third hike, The Grand Canyon of the Yellowstone (Yellowstone NP, Wyoming) On our third hike, The Grand Canyon of the Yellowstone
(Yellowstone NP, Wyoming)
On our third hike, the upper falls (Yellowstone NP, Wyoming) On our third hike, the upper falls
(Yellowstone NP, Wyoming)
Waiting for Old Faithful (Yellowstone NP, Wyoming) Waiting for Old Faithful
(Yellowstone NP, Wyoming)
Old Faithful (Yellowstone NP, Wyoming) Old Faithful
(Yellowstone NP, Wyoming)
Old Faithful Inn (Yellowstone NP, Wyoming) Old Faithful Inn
(Yellowstone NP, Wyoming)
27ºF (Yellowstone NP, Wyoming) 27ºF
(Yellowstone NP, Wyoming)
Early summer flowers (Yellowstone NP, Wyoming) Early summer flowers
(Yellowstone NP, Wyoming)
Mammoth Hot Springs (Yellowstone NP, Wyoming) Mammoth Hot Springs
(Yellowstone NP, Wyoming)
Dried up hot springs (Yellowstone NP, Wyoming) Dried up hot springs
(Yellowstone NP, Wyoming)
European tourists (Yellowstone NP, Wyoming) European tourists
(Yellowstone NP, Wyoming)
The Golden Gate (Yellowstone NP, Wyoming) The Golden Gate
(Yellowstone NP, Wyoming)
Looking for the elusive sage moose (Alces sagifora) (Yellowstone NP, Wyoming) Looking for the elusive sage moose Alces sagifora
(Yellowstone NP, Wyoming)
On our fourth hike, the summer trip to Mordor (Yellowstone NP, Wyoming) On our fourth hike, the summer trip to Mordor
(Yellowstone NP, Wyoming)
Lower falls of the Yellowstone (Yellowstone NP, Wyoming) Lower falls of the Yellowstone
(Yellowstone NP, Wyoming)
The Beartooth Highway, Wyoming The Beartooth Highway, Wyoming
Baby cousin (Billings, Montana) Baby cousin
(Billings, Montana)
Happy baby, Happy mom (Billings, Montana) Happy baby, Happy mom
(Billings, Montana)
In In “The Montana Room”
(Billings, Montana)
The Montana Capitol (Helena, Montana) The Montana Capitol
(Helena, Montana)
North of Great Falls, Montana North of Great Falls, Montana
On the way to Glacier NP, Montana On the way to Glacier NP, Montana
Blackfeet Nation, Montana Blackfeet Nation, Montana
Snapshooting (Blackfeet Nation, Montana) Snapshooting
(Blackfeet Nation, Montana)
On the way to Saint Mary (Blackfeet Nation, Montana) On the way to Saint Mary
(Blackfeet Nation, Montana)
On our way to Saint Mary (Blackfeet Nation, Montana) On our way to Saint Mary
(Blackfeet Nation, Montana)
The most popular view in the park (Glacier NP, Montana) The most popular view in the park
(Glacier NP, Montana)
Logan Pass (Glacier NP, Montana) Logan Pass
(Glacier NP, Montana)
Logan Pass (Glacier NP, Montana) Logan Pass
(Glacier NP, Montana)
Clearing storm, Gunsight Pass (Glacier NP, Montana) Clearing storm, Gunsight Pass
(Glacier NP, Montana)
Gunsight Pass (Glacier NP, Montana) Gunsight Pass
(Glacier NP, Montana)
Beautiful mountain weather (Waterton Lakes NP, Alberta) Beautiful mountain weather
(Waterton Lakes NP, Alberta)
The Prince of Wales Hotel (Waterton Lakes NP, Alberta) The Prince of Wales Hotel
(Waterton Lakes NP, Alberta)
Bear grass (Waterton Lakes NP, Alberta) Bear grass
(Waterton Lakes NP, Alberta)
On our fifth hike, clearing snow (Glacier NP, Montana) On our fifth hike, clearing snow
(Glacier NP, Montana)
On our fifth hike, Mount Oberlin (Glacier NP, Montana) On our fifth hike, Mount Oberlin
(Glacier NP, Montana)
On our fifth hike, the boardwalk (Glacier NP, Montana) On our fifth hike, the boardwalk
(Glacier NP, Montana)
On our fifth hike, the snowfield (Glacier NP, Montana) On our fifth hike, the snowfield
(Glacier NP, Montana)
On our fifth hike, the baby mountain goat (Glacier NP, Montana) On our fifth hike, the baby mountain goat
(Glacier NP, Montana)
Merriwether Luis, the wayward conquistador (Marias Pass, Montana) Merriwether Luis, the wayward conquistador
(Marias Pass, Montana)
Petting a sea star (Seattle, Washington) Petting a sea star
(Seattle, Washington)
Funky coral (Seattle, Washington) Funky coral
(Seattle, Washington)
Happy to have coffee at the Space Needle (Seattle, Washington) Happy to have coffee at the Space Needle
(Seattle, Washington)
Happy in-laws (Seattle, Washington) Happy in-laws
(Seattle, Washington)
Calder stabile (Seattle, Washington) Calder stabile
(Seattle, Washington)
Look who's smart enough to wear a hat to Safeco (Seattle, Washington) Look who’s smart enough to wear a hat to Safeco
(Seattle, Washington)
Another great evening for a great baseball game (Seattle, Washington) Another great evening for a great baseball game
(Seattle, Washington)
Doing the large format thing (Mount Saint Helens National Volcanic Monument, Washington) Doing the large format thing
(Mount Saint Helens National Volcanic Monument, Washington)
Mid-summer wildflowers (Mount Saint Helens NVM, Washington) Mid-summer wildflowers
(Mount Saint Helens NVM, Washington)
Mid-summer wildflowers (Mount Saint Helens NVM, Washington) Mid-summer wildflowers
(Mount Saint Helens NVM, Washington)
A Zen moment at the Japanese Garden (Portland, Oregon) A Zen moment at the Japanese Garden
(Portland, Oregon)
Mind like water (Portland, Oregon) Mind like water
(Portland, Oregon)
Watching the sea lions (Portland, Oregon) Watching the sea lions
(Portland, Oregon)
Sea lions (Portland, Oregon) Sea lions
(Portland, Oregon)
Crocs (Portland, Oregon) Crocs
(Portland, Oregon)
Posted in Photography, Travel, USA, Western Adventure | 1 Comment

Coming Soon: What I Did on my Summer Vacation


Blackfeet Nation, Montana

It’s taking me longer than I had hoped, but you can expect to see photographs and details about our Western swing soon. . . .

Posted in Photography, Travel, USA, Western Adventure | Leave a comment