MATLAB: How do I fix subscripted assignment dimension mismatch? -


new_img ==>

new_img = zeros(height, width, 3);  

curmean this: [double, double, double]

new_img(rows,cols,:) = curmean; 

so wrong here?

the line:

new_img(rows,cols,:) = curmean; 

will work if rows , cols scalar values. if vectors, there few options have perform assignment correctly depending on sort of assignment doing. jonas mentions in answer, can either assign values every pairwise combination of indices in rows , cols, or can assign values each pair [rows(i),cols(i)]. case assigning values every pairwise combination, here couple of ways can it:

  • break assignment 3 steps, 1 each plane in third dimension:

    new_img(rows,cols,1) = curmean(1);  %# assignment first plane new_img(rows,cols,2) = curmean(2);  %# assignment second plane new_img(rows,cols,3) = curmean(3);  %# assignment third plane 

    you in loop jonas suggested, such small number of iterations kinda use "unrolled" version above.

  • use functions reshape , repmat on curmean reshape , replicate vector matches dimensions of sub-indexed section of new_img:

    nrows = numel(rows);  %# number of indices in rows ncols = numel(cols);  %# number of indices in cols new_img(rows,cols,:) = repmat(reshape(curmean,[1 1 3]),[nrows ncols]); 

for example of how above works, let's have following:

new_img = zeros(3,3,3); rows = [1 2]; cols = [1 2]; curmean = [1 2 3]; 

either of above solutions give result:

>> new_img  new_img(:,:,1) =       1     1     0      1     1     0      0     0     0  new_img(:,:,2) =       2     2     0      2     2     0      0     0     0  new_img(:,:,3) =       3     3     0      3     3     0      0     0     0 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -