フィルターのクリア

How to assign parts of a matrix equal to a single vector

23 ビュー (過去 30 日間)
David
David 2012 年 4 月 22 日
In matlab if you had a vector called y = ones(5,5), you could do the following assignments:
y(:,1) = 0; First column in all rows equals zero.
y(:,1:2) = 0; First two columns in all rows equals zero.
y(1,:) = 0; First row equals zero, etc.
But what if you wanted to be more specific, say for example I had a vector x = [0 2 0] and I wanted y(2:4,2:4) = x; Meaning the middle 3 columns and middle 3 rows would be set to that vector. The problem is it doesn't accept this kind of assignment and gives a "Subscripted assignment dimension mismatch" error. I was wondering if there is any way to do this, or this something like this only possible through a for loop?

採用された回答

Richard Brown
Richard Brown 2012 年 4 月 22 日
It's because y(2:4, 2:4) is a 3x3 matrix, and so you must assign it a 3x3 matrix. The command repmat is an easy way to stack multiple copies of a matrix together. Assuming you want each of the rows of that 3x3 block to be [0 2 0]:
x = [0 2 0];
y(2:4, 2:4) = repmat(x, 3, 1);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by