How to replace element matrix with the other matrix?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
What if I have a matrix A (4x4) with a size of (1x64 cell), and the matrix B (2x2) with size of (1x64 cell)? And I want to replace all of the matrix A.
7 commentaires
Bjorn Gustavsson
le 7 Fév 2014
OK, this doesn't really work if you cant communicate clearly what A and B are. Instead of describing in words, paste the output of
whos A B
and in case A and B are a cell arrays also
size(A{1})
size(B{1})
Réponses (6)
Image Analyst
le 7 Fév 2014
OK, so both A and B are 1 by 64 cell arrays. And you say "And I want to replace all of the matrix A." Presumably replace A with B. So you do this:
A = B;
2 commentaires
Image Analyst
le 8 Fév 2014
A is NOT 4 by 4. It is 1 by 64. You don't understand the concept of cell arrays. I really really suggest you read the FAQ on it: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
Plus, you and I must have different definitions of "all". To me, your "all" means a subset (2x2 out of 4x4).
Bjorn Gustavsson
le 7 Fév 2014
OK, then I suggest that (unless you definitely need the cell-array A as a cell-array) you first change A and B to 3-D matrices and put in B in A:
A = cat(3,A{:});
B = cat(3,B{:});
A(2:3,2:3,:) = B;
HTH.
2 commentaires
Bjorn Gustavsson
le 8 Fév 2014
Well, are you sure that B is a cell array at the time you call cat and you have the same sized (2 x 2) arrays in all cells of B?
The solution I suggested works in matlab 2013a...
Jos (10584)
le 7 Fév 2014
I suggest that you give a smaller example of A and B and the required output.
3 commentaires
Jos (10584)
le 8 Fév 2014
So you want the central portion of B to be replaced with A.
What do you mean with "I have 64 pieces of both that matrix"?
Bjorn Gustavsson
le 8 Fév 2014
Well that's completely opposite of your initial description of the sizes...
B(2:3,2:3) = A;
Would be what you'd do for that. For the case that B is 3-D you'd modify it to:
B(2:3,2:3,:) = A;
Walter Roberson
le 8 Fév 2014
newA = cellfun(@(a,b) subsasgn(a, struct('type', '()', 'subs', {2:3, 2:3}), b), A, B, 'Uniform', 0);
I think.
2 commentaires
Walter Roberson
le 8 Fév 2014
newA = cellfun(@(a) subsasgn(a, struct('type', '()', 'subs', {2:3, 2:3}), B), A, 'Uniform', 0);
Jos (10584)
le 8 Fév 2014
To replace the central portion of B with A try this
% example data
A = 2 * ones(2,2)
B = -3 * ones(6,8)
% engine
szA = size(A)
szB = size(B)
tf = false(szB)
tf(1:szA(1),1:szA(2)) = true
tf = circshift(tf,(szB-szA)/2)
B_output = B
B_output(tf) = A
0 commentaires
Jos (10584)
le 8 Fév 2014
Even simpler:
% example data
A = 99 * ones(2,2)
B = -1 * ones(6,8)
% engine
szA = size(A)
i0 = (size(B)-szA)/2
B_output = B ;
B_output(i0(1)+1:i0(1)+szA(1), i0(2)+1:i0(2)+szA(2)) = A
0 commentaires
Voir également
Catégories
En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!