Replacing single values in a matrix with 1x09 doubles

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!

4 Comments

Clarifying question: The array designs is a 6x15 numeric array. Do you want the final output to be a 6x31 numeric array, because what used to be a single element has been replaced with a 1x9 vector?
If not, what do you mean?
Thank you for reaching out!
I believe I have confused myself here too (I am fairly new to programming).
The desired outcome would be a 6x15 cell array, with each cell being 1x9.
And when counting the instances of "1" in the designs array, do you want to work your way across the rows, or down the columns.
In other words, is the "second 1" in the first row, or in the third column?
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.

Sign in to comment.

 Accepted Answer

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

3 Comments

Thank you so much! I will want to iterate through all numbers.
I just decided to try this code on a new tab in matlab, and I'm getting the following error:
Index exceeds the number of array elements (1).
Error in untitled3 (line 21)
for ii = 1:numel(ir)
Did you define a variable called "numel"? You shouldn't do that, because it conflicts with the function name.
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.

Sign in to comment.

More Answers (0)

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!