Indexing a multidimensional matrix with a second logical matrix

2 次查看(过去 30 天)
Hello,
I want to setup dummy orientation given a binary image.
I have a binary images (data) with withe regions separated from each other with black borders.
I now want to setup the orientation data that each white pixel has the oriantation value zeros(3,3) and each black pixel has ones(3,3).
So I startet with:
ori=zeros(3,3,size(data,1),size(data,2));
ori(:,:,data==0)=ones(3,3);
but that doenst work.
So even so data==0 is a 2d logical matrix, within ori(:,:,data==0) it becomes linear and I get the error that left site is 3-by-3-by 14262 (sum of 0 elements in data) and right side only 3by3.
How can I bypass that, or is it even possible to do is by logical ndexing without looping row or columnwise??
Many thanks in advance

采纳的回答

Walter Roberson
Walter Roberson 2022-2-8
编辑:Walter Roberson 2022-2-8
mask = data==0;
ori(:,:,mask) = ones(3,3, nnz(mask)) ;
However I would want to test this. If it works it would likely only be because the values being copied are all the same; if there was a pattern being set such as a cross, then my suspicion is that the memory locations would be out of order.

更多回答(0 个)

类别

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

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by