how to combine two matrices?

2 ビュー (過去 30 日間)
arian hoseini
arian hoseini 2022 年 1 月 10 日
コメント済み: arian hoseini 2022 年 1 月 11 日
A=[Pg; 0.5;0.6;0;0;0]
B=[0;0;0;0.7;0.7;0.7]
1)the answer i need is C=[Pg;0.5;0.6;0.7;0.7;0.7]
i tried C=[A,B] but thats not what i want.
2)how can i add zero to matrix D if first value of A is Pg.

採用された回答

Les Beckham
Les Beckham 2022 年 1 月 10 日
Maybe this is what you want for #1:
Pg = 1; % example value for Pg
A=[Pg; 0.5;0.6;0;0;0]
A = 6×1
1.0000 0.5000 0.6000 0 0 0
B=[0;0;0;0.7;0.7;0.7]
B = 6×1
0 0 0 0.7000 0.7000 0.7000
C = A+B
C = 6×1
1.0000 0.5000 0.6000 0.7000 0.7000 0.7000
For #2:
if A(1) == Pg
% Do something to D
end
  3 件のコメント
Les Beckham
Les Beckham 2022 年 1 月 11 日
編集済み: Les Beckham 2022 年 1 月 11 日
It really depends on what you want to do. In the specific example you give in the question the first three elements of B and the last three elements of A are zero so adding them essentially replaces those elements. This is true regardless of the sign of Pg.
If you want to combine a portion of A with a portion of B you could do something like this:
Pg = 1; % example value for Pg
A = [Pg; 0.5; 0.6; 2; 2; 2]; % note that I've replaced the zeros with twos as an example
B = [2; 2; 2; 0.7; 0.7; 0.7];
C = [A(1:3); B(4:6)]
C = 6×1
1.0000 0.5000 0.6000 0.7000 0.7000 0.7000
Note that we get the first three elements of A and the last 3 elements of B.
To learn more of the basics of Matlab (indexing is especially relevant to your questions), I suggest that you take advantage of the free online training available here: https://www.mathworks.com/learn/tutorials/matlab-onramp.html
arian hoseini
arian hoseini 2022 年 1 月 11 日
thank u ...u are a life savior.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by