put smaller matrix in bigger matrix

2 views (last 30 days)
Hello I have row=[1 2 5 4 3 6] and column=[5 7 3 4 8 14]. I want to select and put each Q(row,column) in a new data. for example Q(row(1),column(1)), Q(row(2),column(2)), and Q(row(3), column(3)), .. . I want to put these Q in a new matrix P. Can anybody help me? something like this that does not work: P=Q((1:row),(1:column)) or P=Q(row,column)
  2 Comments
Rahul
Rahul on 30 May 2012
How do you want output
q= 1 5
2 7
5 3
4 4
3 8
6 14
Is this how your output q should look like? give an example of your output..
Andrea
Andrea on 30 May 2012
No. These are the indicators of row and col. for example Q(row(1),column(1))=10, Q(row(2),column(2))=25 , and Q(row(3), column(3))=12 and ....
I want to to put them in a matrix P=zeros(size(Q)). in the way their arrangement will not change.
please see this example:
Q =
1 2 3 4 5 6
3 2 1 6 5 4
0 1 10 5 5 1
8 7 9 8 1 0
row = [2 3];
col= [ 4 5 6];
P =
6 5 4 0 0 0
5 5 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
and then, I want to select another sub-matrix (with different size such as 1*2) and put it right next to the first part in Matrix P.

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 30 May 2012
eg:
Q = randi(456,10,15);
row=[1 2 5 4 3 6];
column=[5 7 3 4 8 14];
P = Q(sub2ind(size(Q),row,column));
EDIT
p = Q(row,column);
P = zeros(size(Q));
P(1:numel(row),1:numel(column)) = p;
  3 Comments
Andrea
Andrea on 30 May 2012
Just one issue occurs. I need matrix p to have Q(row(1),column(1)), Q(row(1),column(1)), Q(row(2),column(2)), Q(row(3),column(3)), Q(row(4),column(4)), Q(row(5),column(5)), and Q(row(6),column(6)). But what (p = Q(row,column);) give me is a 6*6 matrix in this way Q(row(1),column(1)), Q(row(1),column(2)), Q(row(1),column(3)), Q(row(1),column(4)), and..... How can solve this problem?
Andrei Bobrov
Andrei Bobrov on 31 May 2012
Hi Andrea! Please see my answer (befor 'EDIT')

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!