How to replace element matrix with the other matrix?

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

Amit
Amit le 7 Fév 2014
what do you mean matrix A (4x4) with a size of (1x64 cell) and the same goes for B (2x2) with size of 1x64 cell?
I don't understand. Are A and B cell arrays, or regular numerical arrays (int32 or double)? Is A a 1x64 cell array and in every one of those cells is a 4x4 array of doubles? Your explanation is not clear.
I mean matrix A with size 4x4, but it have 64 pieces or block. And the matrix B with size 2x2, and it have 64 pieces or block, both have cell arrays. So I want to replace the middle of matrix A(size 2x2) with the matrix B.
Wouldnt this do it:
A(2:3,2:3) = B;
Andi Tarigan
Andi Tarigan le 7 Fév 2014
Modifié(e) : Andi Tarigan le 7 Fév 2014
How if I have 64 blocks of matrix A and matrix B with type cell arrays? But every blocks of matrix have type 4x4 double for matrix A and 2x2 double for matrix B. Please help me..
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})
Do you mean like this?
>> A = Mmid1_blue_new;
>> whos A
Name Size Bytes Class Attributes
A 1x64 9216 cell
>> B = blok_blue_dwt_dct_HL13;
>> whos B
Name Size Bytes Class Attributes
B 1x64 15360 cell

Connectez-vous pour commenter.

Réponses (6)

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

But the size different, matrix A is 4x4 and matrix B 2x2. I just want to replace the middle matrix A(size 2x2) with matrix B.
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).

Connectez-vous pour commenter.

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

But I get error when I type:
>> B = cat(3,B{:});
Cell contents reference from a non-cell array object.
and
>> A(2:3,2:3,:) = B;
Subscripted assignment dimension mismatch.
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...

Connectez-vous pour commenter.

Jos (10584)
Jos (10584) le 7 Fév 2014

0 votes

I suggest that you give a smaller example of A and B and the required output.

3 commentaires

A =
36.2700 10.5400
38.2800 59.8200
B =
-9.7800 -31.4500 79.9600 -2.6000
-39.8500 35.8600 10.0700 -6.4300
-91.7800 37.8300 59.3400 -35.3200
15.2500 -29.0500 52.4800 -16.6700
B_output =
-9.7800 -31.4500 79.9600 -2.6000
-39.8500 36.2700 10.5400 -6.4300
-91.7800 38.2800 59.8200 -35.3200
15.2500 -29.0500 52.4800 -16.6700
But, I have 64 pieces both of that matrix.
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"?
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;

Connectez-vous pour commenter.

newA = cellfun(@(a,b) subsasgn(a, struct('type', '()', 'subs', {2:3, 2:3}), b), A, B, 'Uniform', 0);
I think.

2 commentaires

But I get error when type it:
>> newA = cellfun(@(a,b) subsasgn(a, struct('type', '()', 'subs', {2:3, 2:3}), b), A, B, 'Uniform', 0);
Error using cellfun
Input #2 expected to be a cell array, was double instead.
newA = cellfun(@(a) subsasgn(a, struct('type', '()', 'subs', {2:3, 2:3}), B), A, 'Uniform', 0);

Connectez-vous pour commenter.

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
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

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by