Matlab: find a string within a cell array of strings

I just wanted to jot down a few points about Matlab programming. Specifically, this is about finding a string within another cell array of strings, where the thing I’m really interested in is the index of the cell array where the reference string occurs. For example, if my reference string is 'Gamma', and my cell array is {'Alpha','Beta','Gamma','Delta'}, then the result of the code should be 3.

Say,

cellArray = {'Alpha','Beta','Gamma','Delta','GammaSquared'};
refString = 'Gamma';

Method 1

This method uses the Matlab function strfind (link).

index = strfind(cellArray,refString);
index = find(~cellfun(@isempty,index));

Result:

index = 
    3   5

This method works great if the idea is to find a substring, i.e. in the case where we are looking for all possible matches. It doesn’t work too well, however, if we’re looking for a specific match.

Method 2

This uses the Matlab function ismember (link).

index = find(ismember(cellArray,refString));

Result:

index = 
    3

Works great if the idea is to find a perfect match. However, let’s also keep tabs on the computation time.

tic; index = find(ismember(cellArray,refString)); toc;

Result:

Elapsed time is 0.001047 seconds.

Method 3

This uses the Matlab function strcmp (link).

index = find(strcmp(cellArray,refString));

Result:

index = 
    3

Same result as in Method 2, but what about computation time?

tic; index = find(strcmp(cellArray,refString)); toc;

Result:

Elapsed time is 0.000025 seconds.

Turns out Method 3 is more than 41 times faster to execute. So we have a winner!

Reference

Stack Overflow: How to search for a string in cell array in MATLAB?


More on the Naked Mole Rat

I’ve written about the naked mole rat before, about how it seems to be immune to acid.

Well, it turns out it has more tricks up its genetic sleeve.

To compare how the naked mole rat made their proteins, they inserted an engineered gene in the naked mole rat as well as in mice, which allowed them to compare the rate of errors in making proteins. And here’s what they found:

[The naked mole rat] built the engineered protein far more accurately, in other words. Naked mole rats, the scientists found, made anywhere from four to ten times fewer mistakes. Yet the naked mole rats can make their proteins as quickly as the sloppier mice.

This seems to be a fascinating creature the more we study it!

I wonder, though, why other species did not pick up this brilliant piece of evolution. Are there side effects to this that are detrimental, overall, to other species but which don’t affect the naked mole rat? As I said in my earlier post, intriguing.

(Original source, quoted by National Geographic: Jorge Azpurua et al.“Naked mole-rat has increased translational fidelity compared with the mouse, as well as a unique 28S ribosomal RNA cleavage.” PNAS 2013


Dissertation Proposal

I defended my dissertation proposal today.

What this means is that I now have an approved (by my dissertation advisory committee) framework for the research that will grant me my Ph.D.

There were no bad surprises that caught me or my adviser off guard. My advisory committee members are really great people, and we were able to discuss what I’m proposing to do very well. They suggested a couple of things, but they were ideas that me and my advisor had been discussing ourselves only recently, so even that wasn’t out of the blue for us. Although—to an extent we were, or at least I was, thinking of doing those things for different reasons than what the committee suggested. That’s the thing with these defenses—you get a different perspective on what you look at everyday.

Well, one more step completed. This was on the calendar for quite a long time, and it’s nice to get this done. :)

On the other hand, this is the beginnning of the next, arduous path—now that I’ve proposed something, I’ve to actually go ahead and do those things! Lots of experiments to do; many lines of MATLAB code to write and debug—fun times are ahead!

But for now—a bit of a break. Well earned, if I do say so myself. :)



It looked fine from the outside.’

An old building in Secunderabad collapsed today. The building was used as a hotel, which means that quite a lot of people were inside when it happened. Although some of those people have been rescued, 11 people have already been declared dead, and many more are still trapped inside.

Some points from the report–

  • The hotel was planning to build an oven on the first floor (this is India, which means they’re talking about the ‘second floor’ in American parlance). The building may have collapsed under the weight of bricks being transported for this; the building lacked concrete pillars to support the weight.
  • The building had structural weaknesses, but the hotel was not issued safety notices (recently, along with many other buildings) because “the building looked fine from outside”.
  • In future, all buildings over 60 years old will be given orders for renovation,” says the city planning commissioner.

This is appalling, on a number of counts.

  • Did the hotel seek permission from the city planning commissioner’s office before undertaking their oven-building project? If not, this is gross negligence on the part of the hotel authorities. They are presumably not experts in structural engineering, and should not be in a position to judge the capabilities of a structure.
  • Looked fine from the outside’ is not a structural health monitoring technique. However much the city planning commission tries to blame anyone else, they were grossly negligent too. Laziness in health monitoring of public structures causes lives, and they should know and acknowledge that.

    Health monitoring norms and procedures are in place for a reason, and even the most exhaustive and advanced health monitoring techniques don’t work if ‘looked fine from the outside’ is treated as an adequate criterion.

  • How about, instead of treating every building older than 60 years as needing renovation, the city planning commission does its job of inspecting buildings thoroughly? The problem isn’t that they can’t identify structural flaws—they’ve recently issued notices to 57 other buildings recently. It’s just that this building fell through the cracks, because ‘it looked fine from the outside’.

    The problem with the diktat about 60 year old buildings is that it increases the overall cost of maintaining buildings, and decreases the efficiency of health checks. Even well designed buildings, which are for all intents in great shape, will be forced to undergo renovations, or at least exhaustive checks, when they clearly don’t need them. On the one hand this increases the overall cost of inspection and renovation, because you’re spending resources where they’re not needed. On the other it increases the risk of ‘it looks good from the outside’ assessments. This is because the more the number of buildings tested that turn out to be perfectly healthy, the more the officers are prone to become lax and take shortcuts in making assessments.

    Instead, why not improve the work ethic of the inspection officials? Why not make them actually follow the health monitoring guidelines, and make sure that buildings that actually need detailed renovations are the only ones that get notices?

Can you tell that the line about ‘it looked fine from the outside’ has irked me? It has. Structural health monitoring is extremely important for public safety, and such negligence should have no place here. Compare this with a recent tragic story of a building inspector in Philadelphia, USA, who apparently committed suicide because a building collapsed under his watcheven though he had nothing to do with it:

A city building inspector who examined a demolition project just weeks before a building collapsed at the site and killed six people has been found dead in an apparent suicide […]

This man did nothing wrong,” Mr. Gillison [deputy mayor for public safety and chief of staff] added. “The department did what it was supposed to do under the code at the time.” Officials have said they will make any necessary changes to the city’s building code to prevent such incidents in the future.

Some accidents, like the one in Philadelphia, are unavoidable, and it is indeed tragic when the building inspectors blame themselves. The whole point of health and safety standards, and inspection codes and permissions, is to avoid accidents where possible, and the whole system stands on inspectors and officials with acute eyes and sharp skills.

The collapse in Secunderabad is sad and tragic, but the most important question is and should be—was it avoidable? Apparently it was. Does Secunderabad need a change in safety standards? Only the planning commissioner’s office can determine that—although they’d do well to remove ‘it looked fine from the outside’ from their manuals.