我有一个空白img,其中包含大小为x,y,3的所有零。 我有一个线性索引数组,例如
[1 10 99 1562]
我将颜色设置为1、1、3双精度数组,例如
color(:,:,1) = 100
color(:, :, 2) = 200
color(:,:,3) = 100
如何将img的所有索引设置为该颜色?
答案 0 :(得分:2)
带有循环:
[r, c] = ind2sub(size(A(:,:,1)),ind); %Getting corresponding row and column subscripts
for k = 1:numel(ind)
A(r(k),c(k),:) = color; %Changing each of them to desired color
end
或向量化解决方案:
%Creating a mask
mask = false(size(A(:,:,1))); mask(ind) = true;
%Transferring the color to the mask
A = mask.*color; %Impl.exp., use A = bsxfun(@times,mask,color) in <R2016b