Replace portion of array with another array of different size

10 ビュー (過去 30 日間)
Nathaniel H Werner
Nathaniel H Werner 2018 年 9 月 21 日
編集済み: Stephen23 2018 年 9 月 21 日
I have this grid
A = 0:0.01:1;
And I want to refine the grid but only in a small part where some interesting things happen.
So I made the following.
q = 1/16;
S = 0.25;
tq = A(A>=S-q & A<=S+q);
t = linspace(tq(1),tq(end),75);
I want to replace part of A with t, but the part I want replace is a different length array than t.
A(A>=S-q & A<=S+q) = t
gives the error
In an assignment A(:) = B, the number of elements in A and B must be the same.
How can I work around this?

採用された回答

Stephen23
Stephen23 2018 年 9 月 21 日
編集済み: Stephen23 2018 年 9 月 21 日
You cannot replace part of an array with another array that has a different number of elements (unless the replacement is a scalar). But you can easily use indexing to get the parts of the array that you want to keep, and concatenate them together to create a new array:
q = 1/16;
S = 0.25;
A = 0:0.01:1;
L = find(A>=S-q,1,'first');
R = find(A<=S+q,1,'last');
V = linspace(A(L),A(R),75);
B = [A(1:L-1),V,A(R+1:end)]

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by