How do I create an empty array that will store my characters?

23 ビュー (過去 30 日間)
Lamba Opa
Lamba Opa 2018 年 1 月 25 日
コメント済み: Walter Roberson 2018 年 1 月 25 日
I want to store these characters G1,H4,Ia,Jb,Kc in to an array by using a for loop, how do I do it?

採用された回答

Walter Roberson
Walter Roberson 2018 年 1 月 25 日
chars = 'G1,H4,Ia,Jb,Kc';
numchars = length(chars);
newchars = blanks(numchars);
for K = 1 : numchars
newchars(K) = chars(K);
end
  2 件のコメント
Lamba Opa
Lamba Opa 2018 年 1 月 25 日
編集済み: Lamba Opa 2018 年 1 月 25 日
I want to use a for loop to iterate through a numeric array like [1 2 3 4], and if it equals 4 then I want the new array of the same position as the first array to be G1 so element 3 of my new array to be G1. Thank you for your first response!
Would I need to create an empty array?
Walter Roberson
Walter Roberson 2018 年 1 月 25 日
codes = {'G1', 'H4', 'Ia', 'Jb', 'Kc'};
key_value = [ 4, 7, 2, 5, 11];
numeric_array = randi(11, 1, 10); %sample array
new_array = cell(1, length(numeric_array));
[mask, idx] = ismember(numeric_array, key_value);
new_array(mask) = codes(idx(mask));
That is, you indicated that each entry in the string array has a numeric value associated with it, and the string entry is to be used in places that the numeric array matches the special numeric values. So in the above example, 'G1' is to be used for places the numeric array are 4, and 'H4' are to be used for places the numeric array are 7.
The slots for which the numeric array does not match one of the keys are filled with [], the empty numeric array.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by