How do I change Array element with another array element?

30 ビュー (過去 30 日間)
Nick
Nick 2022 年 11 月 16 日
回答済み: Voss 2022 年 11 月 16 日
I am new to matlab and I apologize if my question confuses you.
Basically, if i have an array A = [0 0 0 0] and then I want to replace each value with another Array B [1 1 0 0] how would I go about that? The reason for this is because A is a temp array and I would be adding it into another larger array, so a 2d matrix.
so B is an input to a function, and I am replacing the values of A with B, then doing some math operations and then adding the final product to another Array C.
Thank you,

回答 (2 件)

David Hill
David Hill 2022 年 11 月 16 日
A = [0 0 0 0];
B = [1 1 0 0];
A=B
A = 1×4
1 1 0 0

Voss
Voss 2022 年 11 月 16 日
A = [0 0 0 0];
B = [1 1 0 0];
To replace each element of A with the corresponding element of B:
A = B; % since A and B are the same size, just replace all of A with B
or:
A(:) = B; % to preserve the size of A when B has different size
% (as long as the number of elements is the same)
An example of the second usage:
A = [0 0 0 0] % 1-by-4
A = 1×4
0 0 0 0
B = [1 2; 3 4] % 2-by-2
B = 2×2
1 2 3 4
A(:) = B % A stays 1-by-4, but now it contains the elements of B in (column-major) order
A = 1×4
1 3 2 4

カテゴリ

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

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by