How do I replace elements in a vector with other vectors?

2 views (last 30 days)
Cai Chin
Cai Chin on 22 Feb 2021
Answered: Anmol Dhiman on 27 Feb 2021
Hi, I am attempting to replace elements in the vectors seq1, 2 and 3 each with 12 numbers randomly chosen between 1 and 9. I have an array named rectColor1. I am trying to replace elements in the vectors seq1, 2 and 3 with the elements from the rectColor arrays. I have attempted to do so using the following code, but it keeps saying that there are an unequal number of elements on the left and right sides. How would I go about resolving this issue? Any help would be greatly appreciated, thanks in advance:
% Define grayscales
rectColor{1} = [0.1 0.1 0.1];
rectColor{2} = [0.15 0.15 0.15];
rectColor{3} = [0.22 0.22 0.22];
rectColor{4} = [0.30 0.30 0.30];
rectColor{5} = [0.40 0.40 0.40];
rectColor{6} = [0.55 0.55 0.55];
rectColor{7} = [0.7 0.7 0.7];
rectColor{8} = [0.85 0.85 0.85];
rectColor{9} = [1 1 1];
trialorder = [1 2 3 4];
randtemp = Shuffle(trialorder);
% Define distributions
if randtemp(currenttrialinblock) == 1 % No mean difference (control)
seq1 = dis(1, 5, 9, 1, 12);
seq2 = dis(4, 5, 9, 1, 12);
seq3 = dis(1, 5, 9, 1, 12);
elseif randtemp(currenttrialinblock) == 2 % Small mean difference
seq1 = dis(1, 4, 9, 1, 12);
seq2 = dis(4, 5, 9, 1, 12);
seq3 = dis(1, 6, 9, 1, 12);
elseif randtemp(currenttrialinblock) == 3 % Large mean difference
seq1 = dis(1, 3, 9, 1, 12);
seq2 = dis(1, 5, 9, 1, 12);
seq3 = dis(1, 7, 9, 1, 12);
elseif randtemp(currenttrialinblock) == 4 % No mean difference (manipulation)
seq1 = dis(1, 5, 9, 1, 12);
seq2 = dis(4, 5, 9, 1, 12);
seq3 = dis(1, 5, 9, 1, 12);
end
% Substitute numbers for grayscale images
for i = 1:12
seq1(i) = rectColor{seq1(i)};
end
for i = 1:12
seq2(i) = rectColor{seq2(i)};
end
for i = 1:12
seq3(i) = rectColor{seq3(i)};
end
function x = dis(va, mu, ul, ll, nvals)
n = makedist('Normal','mu', mu,'sigma',sqrt(va));
t = truncate(n,ll,ul);
x = floor(random(t,nvals,1) + 0.5);
end

Answers (1)

Anmol Dhiman
Anmol Dhiman on 27 Feb 2021
Hi Cai,
In the above code, rectColor is of type cell whereas seq(1) is of type double. You can verify using
class(rectColor)
To solve the above error you can convert rectColor(i) into array using cell2mat.
seq1(i) = cell2mat(rectColor{seq1(i)});
Hope it Helps

Community Treasure Hunt

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

Start Hunting!