MATLAB: Vectorization – problem with zeros

vectorizationzeros

I need to expand a small matrix A into a bigger one according to a pattern defined by a an array R. But the array may contain zeros so as to know that the rows and columns marked''0'' should be omitted. Here are the matrices:
A=[1 3 2 4; R=[1;
5 6 7 8; 3;
9 1 2 3; 0;
4 5 6 2]; 7];
R'=[1 3 0 7];
% ... the resulting matrix would be:
B=[1 0 3 0 0 0 4;
0 0 0 0 0 0 0;
5 0 6 0 0 0 8;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
0 0 0 0 0 0 0;
4 0 5 0 0 0 2];
B=zeros(7,7); B(R,R')=A ……returns an error

Best Answer

  • Thank you Monkey, I really appreciate your help, but it seems that I've found a more effective solution:*
A=[1 3 2 4;
5 6 7 8;
9 1 2 3;
4 5 6 2];
R=[1 3 0 7];
B=zeros(7);
RNZ=R(R ~= 0);
d=find(R);
T=A(d,d');
B(RNZ,RNZ')=T