Assign value to matrix using vector value as an index
Show older comments
Will appreciate your help in the following problem.
Let's say I have a 3D matrix (Image):
A=zeros(3,3,3)
and I have a column vector with NaNs and numbers:
a=[NaN; 2; 3]
I want to assign to matrix A value of
B=[255, 100, 50]
such that the values of vector "a" provide the column indices for matrix A, NaN values means nothing is asigned. The result would be:
A=[0 0 0; 0 255 0; 0 0 255] [0 0 0; 0 100 0; 0 0 100] [0 0 0; 0 50 0; 0 0 50];
I use the following code, but it runs very long if I process N images in the video..
Im=Some RGB Image; M=size(Im,1);
Vec_ind=Some column vector of size Mx1
B=[255, 100, 50];
for i=1:M
if ~isnan(Vec_ind(i,1))
j_im=Vec_ind(i,1);
Im(i,j_im,1)=B(1);
Im(i,j_im,2)=B(2);
Im(i,j_im,3)=B(3);
end
Accepted Answer
More Answers (0)
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!