I have an R vector and a U vector. The R vector contains mostly zeros but has a couple of values. The U vector is a shorter length than the R vector and contains no zeros. I want to make a U_final vector which has the values of the U vector at the same locations where the R vector values are non-zero. My attempted code is below. It iterates through fine, but the end result is the last value in U in all locations where R is non-zero rather than all values of U in the appropriate non-zero locations of R. I'm using U = [0.0137;0.0081] and R = [0;0;0;72;0;90]. I'd like the output to be U_final = [0;0;0;0.0137;0;0.0081]
for i = 1:length(R) for j = 1:length(U) if R(i,1) ~= 0 U_final(i,1) = U(j,1) else U_final(i,1) == 0 end endend
Best Answer