Inserting an array in another to make an Nested Array
9 次查看(过去 30 天)
显示 更早的评论
Hello I want to build a fractal. I want to start with One=[1 0 1;0 0 0;1 0 1] Zero=zeros(3)
Then I want to search this array for an element =1 if equal to "1" insert the array One otherwise the array Zero. I am pretty sure this can be done with Cat but wanted to see what others think.
Thanks for your time.
2 个评论
James Tursa
2015-2-27
Please provide a complete example of inputs and desired output. "insert the array" really doesn't tell us much about what you really want.
采纳的回答
Guillaume
2015-2-28
C = {zeros(3); [1 0 1; 0 0 0; 1 0 1]};
X = [1 1 0; 0 1 0]; %current matrix
X = cell2mat(C(X+1)) %new matrix
2 个评论
Guillaume
2015-2-28
It's fairly basic indexing, Y = C(X) returns an array Y the size of X (*) where each element Yn is the value of C at index Xn.
Therefore, in C(X+1), when Xn == 0, Yn is C(1) and when Xn == 1, Yn is C(2).
* except under some circumstances (both are vectors) because people at Mathworks love having exceptions which we all know makes generic programming so much easier. /end rant
更多回答(1 个)
Roger Stafford
2015-2-28
编辑:Roger Stafford
2015-2-28
ONE = [1 0 1;0 0 0;1 0 1];
M = (your current matrix of 1's and 0's)
M = kron(M,ONE); % This will be the next version of M
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!