Replacing single values in a matrix with 1x09 doubles
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Matthew Peoples
am 24 Mär. 2021
Kommentiert: Matthew Peoples
am 24 Mär. 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!
4 Kommentare
the cyclist
am 24 Mär. 2021
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?
Akzeptierte Antwort
the cyclist
am 24 Mär. 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
3 Kommentare
the cyclist
am 24 Mär. 2021
Did you define a variable called "numel"? You shouldn't do that, because it conflicts with the function name.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!