Creating a matrix index

3 ビュー (過去 30 日間)
Edward Johnsen
Edward Johnsen 2018 年 9 月 3 日
I have a 10 by 1 matrix of numbers between 1 and 10, lets call it Z. I want to create a matrix, where say it is 5 by 10, where it takes the number from the first row in Z, say 5, and then creates a row in the new 5 by 10 matrix where the first row would be 0 0 0 0 1 0 0 0 0 0 , basically placing a 1 in the column dictated by the number from the Z matrix, can anyone help me?
Cheers, Edward
  1 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 9 月 3 日
Explain: Consider a Matrix as an example and what result you are expecting?

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

回答 (1 件)

Cesar Antonio Lopez Segura
Cesar Antonio Lopez Segura 2018 年 9 月 3 日
編集済み: Cesar Antonio Lopez Segura 2018 年 9 月 3 日
Hi Edward,
Here your question is answered:
Z = [ 5,1,3,6,7,8,9,10,4,6]';
size(Z) % check matrix size
Matrix5by10 = zeros(5,10); % create a zero matrix
% Your question is answered here:
Matrix5by10( 1,Z( 1 ) ) = 1 % write matrix 5 by 10 been Z a colum indicator
If you need a loop, here you can find the solution:
% Iterative example
for i = 1:5; Matrix5by10( i ,Z( i ) ) = 1; end; Matrix5by10

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by