필터 지우기
필터 지우기

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 CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by