Filter löschen
Filter löschen

array assignment question

2 Ansichten (letzte 30 Tage)
athpapa -
athpapa - am 10 Feb. 2011
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..

Akzeptierte Antwort

Matt Tearle
Matt Tearle am 11 Feb. 2011
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 Kommentar
athpapa -
athpapa - am 11 Feb. 2011
thank you very much!!This is what I want...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Rob Graessle
Rob Graessle am 10 Feb. 2011
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 am 10 Feb. 2011
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 Kommentar
athpapa -
athpapa - am 10 Feb. 2011
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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by