Neat way to loop with both index and value in Matlab -


a lot of loops this:

items = [3,14,15,92]; item_i = 1:numel(items)     item = items(item_i);     % ... end 

this looks bit messy me. there loop construct lets me loop through items , carry index @ same time?

i'm looking syntax along lines of for item_i item = items or for [item_i item] = items.

similar chris taylor's answer this:

function [ output ] = enumerate( items ) output = struct('index',num2cell(1:length(items)),'value',num2cell(items)); end   items = [3,14,15,92]; item = enumerate(items)    item.index    item.value end 

the enumerate function need more work general purpose it's start , work example.

this okay small vectors wouldn't want sizable vectors performance issue.


Comments