Replacing single values in a matrix with 1x09 doubles
1 回表示 (過去 30 日間)
古いコメントを表示
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
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?
採用された回答
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
2021 年 3 月 24 日
Did you define a variable called "numel"? You shouldn't do that, because it conflicts with the function name.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!