multi-indexing (slicing) with different slice size
8 views (last 30 days)
Show older comments
Hi, I'm trying to do somethink like that:
img(ind1:ind2, 1, :) = 0 ;
img(ind3:ind4, 50, :) = 0 ;
in one line, is it possible ?
img is an rgb image. i.e 3-dim matrix, ind1 is a starting-index of a row, ind2 is the ending-index of the row. Think about it as I'm filling my image vertically, with different size vectors with different starting point and different ending point (that range is filled with black color).
Now, I'm asking this because I have a vector of starting-indices, a vector of ending-indices and a vector of colume indices,
and I want to color element-wisely each row-starting-index to row-ending-index of each colume, like the example above with two columes: 1 and 50.
for example: row-starting-indices [ind1, ind2, ind3], row-ending-indices [ind4, ind5, ind6], columns [col1, col50, col100]
and I want:
img(ind1:ind4, 1, :) = 0 ;
img(ind2:ind5, 50, :) = 0 ;
img(ind3:ind6, 100, :) = 0 ;
But in one line.
For the above exmaple, I would want something like:
img([ind1 ind2] : [ind3, ind4], [1, 50], :) = 0 ;
But as much as I understand this it is accesing each column with each range and putting there 0, ending with zeros of length of the maximal range in each column,
which is not the desirable result.
Of course it can be done with a for loop on the columns, but can it be done efficiently by appropriate multi-index or something like that ?
Thank you.
0 Comments
Answers (1)
James Tursa
on 19 Feb 2021
You would probably need to generate the linear/logical indexes of the elements involved and then you could assign all of those spots 0 in one line. But it would take a few lines of code, and probably a loop of some sort, to generate all of the linear/logical indexes. So you may as well just assign the 0 spots in a loop.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!