Hi, I have (26 x 1) W = 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46, and want to use X(26 x 2) which is the address matrix for G...
X =
2 2
3 2
4 2
2 3
3 3
4 3
2 4
3 4
4 4
2 5
3 5
4 5
2 6
3 6
4 6
2 7
3 7
4 7
2 8
3 8
4 8
2 9
3 9
4 9
2 10
3 10,
to transpose W onto: G =
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 0 0 0 0 0 0 0 0
-1 0 0 0 0 0 0 0 0 0
-1 0 0 0 0 0 0 0 0 5

 채택된 답변

Star Strider
Star Strider 2016년 5월 13일

1 개 추천

This is a variation on your earlier Question How to index a matrix, and the solution is similar:
%Matrix size
columns=10;
rows=4;
%Blank matrix
X = zeros(4,10);
%Fill matrix (1st row & first column)
newrow =-ones(1,columns); % the row to replace row 1 with
newcolumn=-ones(rows,1); % the column to replace column 1 with
X(1,:)= newrow ; % replace row 1 in a with new
X(:,1) = newcolumn(:); % replace column 1 in a with new
zi = find(X == 0);
X(zi) = [W; 5]'
X =
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 21 24 27 30 33 36 39 42 45
-1 22 25 28 31 34 37 40 43 46
-1 23 26 29 32 35 38 41 44 5

댓글 수: 6

Aswas
Aswas 2016년 5월 13일
Starstrider, can I please have the transposition L-R, rather than top to bottom.
Sure!
Replace the ‘X(zi)’ assignment with:
X(zi) = reshape([W' 5], [], 3)'
to produce:
X =
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 21 22 23 24 25 26 27 28 29
-1 30 31 32 33 34 35 36 37 38
-1 39 40 41 42 43 44 45 46 5
Aswas
Aswas 2016년 5월 13일
편집: Aswas 2016년 5월 13일
Hi Starstrider, when I reduce to 6 columns and 2 rows, I get error: Error using reshape Product of known dimensions, 3, not divisible into total number of elements, 5.
Error in Untitled (line 54) X(zi) = reshape([W' 5], [], 3)'
You have to change the size of the matrix you want reshape to produce. See if this works:
X(zi) = reshape([W' 5], [], 2)'
Here, I tell it to produce a matrix of 2 columns instead of 3 as before, then (as before) use the transpose operator (') to produce the 2-row matrix you want.
Note that the ‘5’ in the vector is the value you want at the end.
See the documentation for the reshape function for details on how to tell it to do what you want. It is versatile, but it has some necessary constraints.
Aswas
Aswas 2016년 5월 13일
Thank you very much Starstrider, sorted.
Star Strider
Star Strider 2016년 5월 13일
As always, my pleasure.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2016년 5월 13일

댓글:

2016년 5월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by