필터 지우기
필터 지우기

array assignment question

조회 수: 2 (최근 30일)
athpapa -
athpapa - 2011년 2월 10일
I have this code:
a = zeros(4, 5);
b = ones(2, 4);
[rows columns] = size(b);
% Define upper left of where I want to place array b
leftColumn = 1;
topRow = 3;
% Do the assignment to place it there.
a(topRow:topRow+rows-1, leftcolumn:leftColumn-columns-1) = b;
disp(a)
How can I change it to define the right Column and not the left? I tried many changes but I have not found it yet! Thank you..

채택된 답변

Matt Tearle
Matt Tearle 2011년 2월 11일
So, this is what you want?
a = zeros(4, 5);
b = ones(2, 4);
[rows columns] = size(b);
% Define upper left of where I want to place array b
rightColumn = 4;
topRow = 3;
% Do the assignment to place it there.
a(topRow:(topRow+rows-1), (rightColumn-columns+1):rightColumn) = b;
disp(a)
Or do you want the columns of b flipped? In which case, just replace = b; with = fliplr(b);
  댓글 수: 1
athpapa -
athpapa - 2011년 2월 11일
thank you very much!!This is what I want...

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

추가 답변 (2개)

Rob Graessle
Rob Graessle 2011년 2월 10일
So you just want to flip the horizontal orientation of b before you insert it into the same location in a?
a(topRow:topRow+rows-1, leftColumn:leftColumn+columns-1) = fliplr(b);
Is that what you want?

Oleg Komarov
Oleg Komarov 2011년 2월 10일
I assume you wanted to do:
A = zeros(4, 5);
B = ones(2, 4);
szA = size(A);
szB = size(B);
% Align to top left
A(1:szB(1),1:szB(2)) = B
A =
1 1 1 1 0
1 1 1 1 0
0 0 0 0 0
0 0 0 0 0
% Align to top right
A(1:szB(1), 1 + szA(2)-szB(2):end) = B
A =
0 1 1 1 1
0 1 1 1 1
0 0 0 0 0
0 0 0 0 0
EDIT #1
If you want to keep exactly your code:
a = zeros(4, 5);
b = ones(2, 4);
[rows columns] = size(b);
% Define upper left of where I want to place array b
leftColumn = 2;
topRow = 3;
% Do the assignment to place it there.
a(topRow:topRow+rows-1, leftColumn:leftColumn+columns-1) = b;
disp(a)
I just changed leftColumn to 2.
Oleg
  댓글 수: 1
athpapa -
athpapa - 2011년 2월 10일
Not exactly. I want with the code that I have to put the table b in the same place as the above code but to start from right to left and not from left to right!If you know how can I do that, you would help me a lot!I have a mistake on my code, the right is:a(topRow:topRow+rows-1, leftColumn:leftColumn+columns-1) = b;
This code put the b table from left to right into the a table. I want the opposite, from right to left, in the same place of the a table!

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by