# Replacing single values in a matrix with 1x09 doubles

2 views (last 30 days)
Matthew Peoples on 24 Mar 2021
Commented: Matthew Peoples on 24 Mar 2021
Hi there, Im really stuck on this and could use some help, so I have a matrix:
designs = [ 0 1 2 3 4 5 6 0 6 5 4 3 2 1 0;
0 2 6 4 1 3 5 0 5 3 1 4 6 2 0;
0 3 2 5 1 6 4 0 4 6 1 5 2 3 0;
0 4 1 5 6 2 3 0 3 2 5 6 1 4 0;
0 5 3 6 1 4 2 0 2 4 1 3 6 5 0;
0 6 5 4 3 2 1 0 1 2 3 4 5 6 0];
and essentially I want to replace each value of every number (i.e. all the ones) with successive 1x09 doubles in a 12x09 array
blockOrderCat =
1×6 cell array
{12×9 double} {12×9 double} {12×9 double} {12×9 double} {12×9 double} {12×9 double}
So the goal is for the first instance of 1 in 'designs' to be replaced with the first 1x09 double in blockOrderCat{1}, the second 1 to be replaced with the second 1x09 double in blockOrderCat{1} etc etc.
I know I have to iterate through both, but I don't know where to begin really.
Thank you so much!
Matthew Peoples on 24 Mar 2021
Working across rows. Second 1 would be the 'second number 1' on the first row, third number 1 would be first 1 on the second row etc.
Thanks for your attention to this and commitment to helping me.

the cyclist on 24 Mar 2021
I think this does what you want, using a straightforward loop.
You could loop this over values of N = 1:6, if what you want to do is replace each number with the corresponding rows from blockOrderCat.
% Which element value to replace
N = 1;
% Inputs
designs = [ 0 1 2 3 4 5 6 0 6 5 4 3 2 1 0;
0 2 6 4 1 3 5 0 5 3 1 4 6 2 0;
0 3 2 5 1 6 4 0 4 6 1 5 2 3 0;
0 4 1 5 6 2 3 0 3 2 5 6 1 4 0;
0 5 3 6 1 4 2 0 2 4 1 3 6 5 0;
0 6 5 4 3 2 1 0 1 2 3 4 5 6 0];
% Create some random data in blockOrderCat. (Use your actual input instead.)
blockOrderCat = cell(1,6);
for nc = 1:6
blockOrderCat{nc} = rand(12,9);
end
% Preallocate the cell array
output = cell(6,15);
% Identify where the 1's are, counting across the rows
[ic,ir] = find(designs'==N);
% Fill in the corresponding elements of the cell array,
% by working down the rows of the first
for ii = 1:numel(ir)
output{ir(ii),ic(ii)} = blockOrderCat{N}(ii,:);
end
Matthew Peoples on 24 Mar 2021
I didn't anywhere in my script, but somehow that was the issue, I cleared it on the command window and it works perfectly. Thank you again so much.

R2020b

### Community Treasure Hunt

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

Start Hunting!