How to replace 2x2 zero sub matrices by different 2x2 matrices ?

How to replace 2x2 zero submatrix of B with 2x2 matrices different matrice.I have 12 matrices of size 2x2 and want to replace zeros of B by those matrices.
B =
0.8776 0.6513 0 0 0 0 0 0
0.0144 0.8646 0 0 0 0 0 0
0 0 0.2943 0.0560 0 0 0 0
0 0 0.1799 0.8169 0 0 0 0
0 0 0 0 0.9263 0.5289 0 0
0 0 0 0 0.0682 0.6944 0 0
0 0 0 0 0 0 0.5811 0.2124
0 0 0 0 0 0 0.6372 0.5433
Thanks

8 Comments

parag - do all 2x2 zero matrices get replaced with the same 2x2 (non-zero) matrix? Or are there different replacement matrices?
yeah,with the same 2x2 (non-zero) matrix.
for eg i have p1 (2x2 matrix)
p1 = a b
c d
B =
0.8776 0.6513 0 0 0 0 0 0
0.0144 0.8646 0 0 0 0 0 0
then i want
0.8776 0.6513 a b 0 0 0 0
0.0144 0.8646 c d 0 0 0 0
Can you describe using matlab syntax (so we can easily copy/paste into matlab) what your inputs are. In your example, above
%this is valid matlab:
B = [0.8776 0.6513 0 0 0 0 0 0
0.0144 0.8646 0 0 0 0 0 0
0 0 0.2943 0.0560 0 0 0 0
0 0 0.1799 0.8169 0 0 0 0
0 0 0 0 0.9263 0.5289 0 0
0 0 0 0 0.0682 0.6944 0 0
0 0 0 0 0 0 0.5811 0.2124
0 0 0 0 0 0 0.6372 0.5433]
In particular, it's not clear how you define your replacements. Have you got 12 replacements matrices? How are they stored? Easiest would be a cell array:
replacements = {[1 2; 3 4], [5 6; 7 8], etc...} %not valid syntax currently!
If it's a cell array how do we know where replacement{5} (for example) goes ?
N = 8
n = 2
Acell=mat2cell(rand(N,n), ones(1,4)*n,n)
celldisp(Acell)
B=blkdiag(Acell{:})
a = 0.01;
for i = 1:4
p1= a*Acell{i}
p2 =a^2*Acell{i}
p3 = a^3*Acell{i}
end
At end I will get 12 matrices and want to replace zero sub matrices of B with those 12 matrices.
basically i want solution in this form
x p1 p2 p3
p1 x p2 p3
p1 p2 x p3
p1 p2 p3 x
At end I will get 12 matrices...
No you will not. The code you have shown produces 3 matrices p1,p2,p3.
for i = 1:4
p1= a*Acell{i}
p2 =a^2*Acell{i}
p3 = a^3*Acell{i}
end
where Acell{i} is a 2x2 matrix and a = 0.01
according to this command i will get 3 (p1 matrices),3 (p2 matrices),
3 (p3 matrices),...so at end 12 matrices.
Could u pls cehck this ( i hve removed semi colons)
The loop would imply that you want to create 12 matrices. However, since it overwrites the matrices in the previous step, you'll end up with only 3 matrices. The ones created on the last step of the loop.
While you say that you have 12 matrices, your I want solution in this form implies that there are only 3 matrices, which are rotated on each row.
A bit confusing as to what you have.
I doubt it will be difficult to do what you want, but the method will vary depending on exactly what it is that you want. So, please clarify.
oops, u r right it will overwites the matrices.Could u pls tell what to do so that I end up with all 12 matrices?
sorry for the confusion, I want this matrix at the end.
x matrix 1 matrix 2 matrix 3
matrix 4 x matrix 5 matrix 6
matrix 7 matrix 8 x matrix 9
matrix 10 matrix 11 matrix 12 x
thanks you so much :)

Sign in to comment.

 Accepted Answer

basically i want solution in this form
x p1 p2 p3
p1 x p2 p3
p1 p2 x p3
p1 p2 p3 x
C={zeros(2), p1,p2,p3};
result=B+cell2mat( C(toeplitz(1:4)) );

3 Comments

Thanks MJ
sorry for the confusion, I want this matrix at the end.
x matrix 1 matrix 2 matrix 3
matrix 4 x matrix 5 matrix 6
matrix 7 matrix 8 x matrix 9
matrix 10 matrix 11 matrix 12 x
Very similar to things I've already mentioned:
B(B==0)=cat(3,matrix4,matrix7,matrix10,matrix1,matrix8,matrix11,...
matrix2,matrix5,matrix12,matrix3,matrix6,matrix9);
thank you so much . :)
haha..sorry for the confusion.
thanks again for all your help..love u :)

Sign in to comment.

More Answers (1)

Here is a simple example, but the right hand side can be any array with an appropriate number of elements
B(B==0)=1:48;

9 Comments

for eg i have p1 (2x2 matrix)
p1 = a b
c d
p2 = a1 b1
c1 d1
B =
0.8776 0.6513 0 0 0 0 0 0
0.0144 0.8646 0 0 0 0 0 0
then i want
0.8776 0.6513 a b a1 b1 0 0
0.0144 0.8646 c d a2 b2 0 0
thanks MJ :)
You said you have 12 matrices in your original post.
B(B==0)=cat(3,p1,p2,p3,...,p12);
Note that it is probably going to continue to be awkward if you hold the matrices in separate variables.
yeah, i have 12 different matrices and want to replaces 12 zeros sub matrices of B with those 12 different matrices...
B(B==0)=cat(3,p1,p2,p3,...,p12);
Note that it is probably going to continue to be awkward if you hold your matrices in separate variables.
N = 8
n = 2
Acell=mat2cell(rand(N,n), ones(1,4)*n,n)
celldisp(Acell)
B=blkdiag(Acell{:})
a = 0.01;
for i = 1:4
p1= a*Acell{i}
p2 =a^2*Acell{i}
p3 = a^3*Acell{i}
end
At end I will get 12 matrices and want to replace zeros of B with that.Is there another way to do this?
Unable to perform assignment because the left and right sides have a different number of elements.
got this error. ;(
How many elements does the right hande side have?

Sign in to comment.

Categories

Asked:

on 18 Mar 2019

Edited:

on 18 Mar 2019

Community Treasure Hunt

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

Start Hunting!