Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
FREEDOM251

Freedom251 › Blog › MATLAB

MATLABでベクトルを作成する方法(ステップバイステップの例付き)

By Freedom251 Team · · 1 min read

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

MATLABで最も基本的なベクトルは、角括弧で作成します。v = [1 2 3 4 5] は 1×5 の行ベクトル、v = [1; 2; 3; 4; 5] は 5×1 の列ベクトルです。行と列の向きは、連結、行列積、関数入力で結果を左右します。この記事では、手入力、コロン演算子、linspace、zeros・ones・rand、転置、サイズ確認までを順に説明します。

MATLABのベクトルとは

ベクトルは1次元の配列です。行ベクトルのサイズは 1×N、列ベクトルは N×1 と表します。MATLABではベクトルも行列と同じ配列として扱われるため、要素数が同じでも向きが違えば別の形状です。配列の基本はMathWorksの行列・配列の説明で確認できます。

As an Amazon Associate I earn from qualifying purchases.

v = [10 20 30];
size(v)      % 1     3
numel(v)     % 3

numel は要素数、size は各次元の大きさを返します。

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

1. 角括弧でベクトルを手入力する

行ベクトル

rowVector1 = [1 2 3 4 5];
rowVector2 = [1, 2, 3, 4, 5];

スペースとカンマは、同じ行に要素を並べます。どちらも 1×5 です。

列ベクトル

columnVector = [1; 2; 3; 4; 5];
size(columnVector)   % 5     1

セミコロンは行を区切ります。セミコロンを使って複数行を作ると、ベクトルではなく行列になります。

A = [1 2 3; 4 5 6];   % 2×3行列

既存のベクトルを連結する

a = [1 2 3];
b = [4 5 6];
c = [a b];    % [1 2 3 4 5 6]
d = [a; b];   % 2×3行列

横方向の連結は列数、縦方向の連結は行数が一致していなければなりません。詳細は配列の作成と連結を参照してください。

2. コロン演算子で規則的な数列を作る

コロン演算子の基本構文は start:step:end です。step を省略すると増分は1になります。

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
x = 1:5;          % [1 2 3 4 5]
y = -2:3;         % [-2 -1 0 1 2 3]
z = 0:2:10;       % [0 2 4 6 8 10]
down = 10:-1:5;   % [10 9 8 7 6 5]

コロン演算子は刻み幅を指定する方法です。ループ範囲やインデックスにも使えます。終点の規則はコロン演算子の公式リファレンスに記載されています。

小数刻みで終点が入らない場合

v = 0:0.3:1;

この場合、1に正確に到達しないため通常は [0 0.3 0.6 0.9] になります。指定した終点を必ず含めたい場合や、浮動小数点の影響を避けて点数を固定したい場合は linspace を使います。

3. linspaceで点数を指定する

linspace(start,end,n) は、開始値と終了値を含む等間隔の値を n 個作ります。

t = linspace(0, 1, 11);
% [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1]
目的 推奨方法
刻み幅を決める start:step:end
要素数を決める linspace(start,end,n)
両端を確実に含める linspace
ループや添字の範囲 コロン演算子

コロン演算子と linspace は通常、行ベクトルを返します。列ベクトルが必要なら tColumn = t.' とします。信号用の時間ベクトルについてはMathWorksの時間ベクトル例も参考になります。

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

4. zeros・ones・randで初期化する

ゼロベクトル

zRow = zeros(1, 5);   % 1×5
zColumn = zeros(5, 1); % 5×1

1のベクトル

oRow = ones(1, 5);
oColumn = ones(5, 1);
q = ones(1, 3, "uint16");

zeros と ones は、必要なサイズを先に確保するときにも便利です。構文はzerosおよびonesのリファレンスで確認できます。

一様乱数ベクトル

rRow = rand(1, 5);
rColumn = rand(5, 1);

rand は0以上1未満の値を生成します。実行ごとに値が変わるため、再現性が必要な例では乱数シードを設定します。

rng(0);
r = rand(1, 5);

5. 行ベクトルと列ベクトルを変換する

転置

row = [1 2 3 4 5];
column = row.';
size(column)   % 5     1

.' は共役を取らない転置です。実数だけなら row' でも同じ結果になりますが、複素数では違いがあります。

z = [1+2i 3+4i];
z.'   % 共役しない転置
z'    % 共役転置

形状だけを変えたい一般的な処理では .' または transpose を使うのが安全です。配列の再配置はMathWorksの解説にまとまっています。

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

reshapeで形状を指定する

v = 1:6;
row = reshape(v, 1, 6);
column = reshape(v, 6, 1);
column2 = reshape(v, [], 1);

reshape 後の要素数は元の要素数と一致しなければなりません。[] を1次元に指定すると、MATLABが残りの次元を計算します。

配列を1本の列ベクトルにする

A = [1 2 3; 4 5 6];
v = A(:);

結果は [1; 4; 2; 5; 3; 6] です。MATLABは列方向に要素をたどるため、行ごとの順序を期待している場合は注意してください。

6. 要素を参照・抽出する

MATLABのインデックスは1から始まります。

v = [10 20 30 40 50];
v(1)       % 10
v(3)       % 30
v(2:4)     % [20 30 40]
v(end)     % 50

w = [1 5 8 2 10];
w(w > 5)   % [8 10]

コロンは配列作成だけでなく添字範囲にも使われます。条件抽出の説明は配列インデックスを参照してください。

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Swift Programming Cheat Sheet Mouse Pad, Quick Reference Guide for Developers, Students & iOS Programmers Essential Computer Accessories for Study, Work, and Reference Purposes NNA
  • Extra Large & Comfortable: Measuring 31.5 x 11.8 inches with a 3mm thickness, this XL mouse pad offers ample space for your mouse, keyboard, and more - ensuring comfort and reducing noise.
  • Smooth & Precise Control: Enjoy effortless mouse movement with the ultra-smooth surface, perfect for both gaming and office work.
  • Non-Slip Rubber Base: The anti-slip rubber base keeps the pad securely in place during intense gaming or work sessions.
  • Durable & Stylish Design: Invisible stitching prevents edge wear while maintaining a sleek look, extending the pad’s lifespan.
  • Waterproof & Easy to Clean: The water-resistant coating allows for quick cleaning—spills and stains wipe away easily.

7. サイズと向きを確認する

v = [1 2 3 4];
size(v)       % [1 4]
length(v)     % 4
numel(v)      % 4
ndims(v)      % 次元数
isvector(v)   % true
isrow(v)      % true
iscolumn(v)   % false

length は最大の配列次元を返すため、形状を厳密に確認するなら size、要素数だけなら numel を使います。関数の一覧はMATLABリファレンスにあります。

実践例1:温度データを入力する

  1. 行ベクトルとしてデータを作ります。

    temperature = [18.5 19.2 20.1 21.0 20.6];
  2. サイズを確認します。

    size(temperature)   % 1     5
  3. 3番目の値を参照します。

    temperature(3)      % 20.1
  4. 列ベクトルに変換し、平均を計算します。

    temperatureColumn = temperature.';
    mean(temperatureColumn)

実践例2:時間ベクトルと正弦波を作る

t = 0:0.01:1;
y = sin(2*pi*5*t);
plot(t, y)
xlabel("Time")
ylabel("Amplitude")
grid on

t は0秒から1秒まで0.01秒刻み、y は同じ要素数の信号です。t と y の長さが一致しないと、プロットでエラーになります。点数を明示するなら次のように書けます。

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
t = linspace(0, 1, 101);

ベクトル計算で向きをそろえる

要素ごとの演算

a = [1 2 3];
b = [4 5 6];
c = a .* b;   % [4 10 18]

2本のベクトルを要素ごとに加算・乗算する場合は、同じ形状にそろえます。

a = [1 2 3];
b = [10; 20; 30];
aColumn = a.';
c = aColumn + b;

内積

a = [1 2 3];
b = [4 5 6];
dotProduct = a * b.';   % 32

* は行列積、.* は要素ごとの乗算です。向きが合わない場合は、転置して意図する演算を明示してください。

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

よくあるエラーと対処

角括弧が閉じていない

v = [1 2 3

Command Windowが入力の継続を待つ場合は Ctrl+C で中断し、v = [1 2 3]; と入力し直します。

行列の次元が一致しない

[a; b] の縦連結では列数、[a b] の横連結では行数をそろえます。加算でも行・列を混在させないでください。

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

reshapeの要素数が合わない

reshape(v,m,n) では m×n が元の要素数と同じでなければなりません。numel(v) で確認してから形状を指定します。

変数の状態を調べる

whos v

古い値を消して再実行する場合は clear v を使います。clear は変数を削除するため、保存していない作業がないか確認してください。

MATLABをインストールせずに試す

ブラウザー版のMATLAB Online基本版は、MathWorks Accountで利用できます。MathWorksの2026年8月18日確認時の案内では、基本版に月20時間、MATLAB Drive 5 GB、10種類の一般的な製品、連続計算時間15分などの制限があります。ライセンス対象者は契約内容に応じてフル版を利用できます。詳細はMATLAB Onlineのバージョン案内で確認してください。

  1. MATLAB Onlineを開き、MathWorks Accountでログインする。
  2. Command WindowまたはLive Editorを選ぶ。
  3. v = [1 2 3 4 5] を入力する。
  4. size(v) で 1×5 を確認する。

30日間の試用版の提供条件は地域・資格で変わる可能性があるため、申込み時に公式の試用案内を確認してください。学生は大学のCampus-Wide Accessを先に確認すると、個人購入なしで利用できる場合があります。学生向け情報はMathWorksの学生ページにあります。

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

用途別の構文早見表

目的 構文 例
手入力の行ベクトル [a b c] [1 2 3]
手入力の列ベクトル [a; b; c] [1; 2; 3]
連続値 start:end 1:10
刻み幅指定 start:step:end 0:0.5:2
点数指定 linspace(start,end,n) linspace(0,1,11)
ゼロベクトル zeros(m,n) zeros(5,1)
1のベクトル ones(m,n) ones(1,5)
乱数ベクトル rand(m,n) rand(1,5)
非共役転置 v.' row.'
形状変更 reshape(v,m,n) reshape(v,5,1)
列ベクトル化 A(:) A(:)

The Bottom Line

刻み幅を決めるならコロン演算子、点数と両端を決めるなら linspace、初期化なら zeros・ones・rand を使います。作成後は必ず size と numel で形状と要素数を確認し、必要に応じて .' または reshape で行・列をそろえてください。

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

MATLABプログラミング入門ベクトル数値計算行列

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.