Replacing single values in a matrix with 1x09 doubles

조회 수: 3 (최근 30일)
Matthew Peoples
Matthew Peoples 2021년 3월 24일
댓글: Matthew Peoples 2021년 3월 24일
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
the cyclist
the cyclist 2021년 3월 24일
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?
Matthew Peoples
Matthew Peoples 2021년 3월 24일
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
the cyclist 2021년 3월 24일
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
the cyclist
the cyclist 2021년 3월 24일
Did you define a variable called "numel"? You shouldn't do that, because it conflicts with the function name.
Matthew Peoples
Matthew Peoples 2021년 3월 24일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by