array assignment question

1 次查看(过去 30 天)
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);

更多回答(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 中查找有关 Title 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by