multi-indexing (slicing) with different slice size

8 次查看(过去 30 天)
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.

回答(1 个)

James Tursa
James Tursa 2021-2-19
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.
  1 个评论
Ilya K
Ilya K 2021-2-19
eventually I did a mask, it was just a few code line and I'm pretty sure it is well parallelizable.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by