difference between a= and a(:)=

8 ビュー (過去 30 日間)
Ernst Reißner
Ernst Reißner 2021 年 6 月 7 日
コメント済み: Ernst Reißner 2021 年 6 月 7 日
I find matlab code with assignments
a(:)=... and a(1:N)=...
where in the latter case the right hand side has length N also.
Why they dont write just a=...
Is there a difference?
Also I found the code snipped
if isvector(V)
V = V(:);
what is the sense of this??

採用された回答

Chunru
Chunru 2021 年 6 月 7 日
a(:) = 1 will not change the size of a, but assigne 1 to every element of a (assume a has been declared before, such as a = zeros(20,1));
On the other hand, a = 1 (for example) will redefine a (both in size, type and value) so that a becomes 1. Even if a is defined earlier as a = zeros(20, 1), a will be redefined with new rhs.

その他の回答 (2 件)

KSSV
KSSV 2021 年 6 月 7 日
編集済み: KSSV 2021 年 6 月 7 日
a(1:N) will be either row or column vector depedning on a is row or column vector respectively.
a(:) always gives you a column vector.
a = rand(1,10) ;
a(1:10)
ans = 1×10
0.6861 0.9950 0.8572 0.1173 0.9764 0.1016 0.8943 0.4731 0.8291 0.0097
a(:)
ans = 10×1
0.6861 0.9950 0.8572 0.1173 0.9764 0.1016 0.8943 0.4731 0.8291 0.0097
If A is a matrix, A(:) will give a column vector will all columns appended one after another.
You can try yourself instead asking such simple questions.

Walter Roberson
Walter Roberson 2021 年 6 月 7 日
Assigning to a(:) or a(1:N) keeps the same "shape" and datatype.
Example:
A1 = zeros(5,1,'uint8');
A2 = zeros(5,1,'uint8');
A1(:) = 1:5;
A2 = 1:5;
After, A1 stays a uint8 column vector, but A2 becomes a double precision row vector.
  1 件のコメント
Ernst Reißner
Ernst Reißner 2021 年 6 月 7 日
also worth accepting

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

カテゴリ

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

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by