replacing numbers with alphabets/letters in a matrix

2 ビュー (過去 30 日間)
ANINDYA GANGULY
ANINDYA GANGULY 2021 年 11 月 8 日
コメント済み: ANINDYA GANGULY 2021 年 11 月 8 日
I have a excel sheet (attached file) consiting of 4000 numbers (1 to 4000). I want to replace 1, 5, 9 ...3997 and 2, 6, 10...3998 with C and 3, 7, 11...3999 and 4,8, 12...4000 with H. Can a code be devised where this task can be performed such that the output file/data is represented in the same array/format as the given input? And can the code be made universal so that we can carry forth similar tasks with other arrays?
Looking forward for an answer and thank you for all your help.
Regards
J
  1 件のコメント
Rik
Rik 2021 年 11 月 8 日
What have you tried yourself already?

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

採用された回答

Akira Agata
Akira Agata 2021 年 11 月 8 日
One possible straight-forward solution would be like this:
M = readmatrix('testfile_0.50.xlsx');
C = cell(size(M));
idx = ismember(M,[1:4:3997, 2:4:3998]);
C(idx) = {'C'};
idx = ismember(M,[3:4:3999, 4:4:4000]);
C(idx) = {'H'};
writecell(C,'output.xlsx');
  1 件のコメント
ANINDYA GANGULY
ANINDYA GANGULY 2021 年 11 月 8 日
Thanks a lot <3. It works

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 11 月 8 日
編集済み: Walter Roberson 2021 年 11 月 8 日
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/793759/testfile_0.50.xlsx';
outfile = "testout.xlsx";
data = readmatrix(filename);
newdata = cell(size(data));
mask = isnan(data);
newdata(mask) = {nan};
mask = ismember(mod(data,4), [1 2]);
newdata(mask) = {'C'};
mask = ismember(mod(data,4), [3 0]);
newdata(mask) = {'H'};
writecell(newdata, outfile);
%testing to see if the output looks ok
readback = readtable(outfile);
readback(10:20,:)
ans = 11×13 table
C C_1 C_2 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 _____ _____ _____ __________ ____ ____ ____ ____ ____ _____ _____ _____ _____ {'H'} {'C'} {'H'} {0×0 char} NaN NaN NaN NaN NaN NaN NaN NaN NaN {'C'} {'C'} {'H'} {0×0 char} NaN NaN NaN NaN NaN NaN NaN NaN NaN {'C'} {'C'} {'C'} {0×0 char} NaN NaN NaN NaN NaN NaN NaN NaN NaN {'C'} {'C'} {'C'} {0×0 char} NaN NaN NaN NaN NaN NaN NaN NaN NaN {'C'} {'C'} {'H'} {0×0 char} NaN NaN NaN NaN NaN NaN NaN NaN NaN {'C'} {'C'} {'H'} {0×0 char} NaN NaN NaN NaN NaN NaN NaN NaN NaN {'C'} {'C'} {'H'} {0×0 char} NaN NaN NaN NaN NaN NaN NaN NaN NaN {'C'} {'C'} {'H'} {'H' } NaN NaN NaN NaN NaN NaN NaN NaN NaN {'C'} {'C'} {'H'} {'H' } NaN NaN NaN NaN NaN NaN NaN NaN NaN {'C'} {'C'} {'H'} {'H' } NaN NaN NaN NaN NaN NaN NaN NaN NaN {'C'} {'C'} {'H'} {'H' } NaN NaN NaN NaN NaN NaN NaN NaN NaN
I tested on my system, and excel does show those 0x0 char and NaN entries as empty cells.
  1 件のコメント
ANINDYA GANGULY
ANINDYA GANGULY 2021 年 11 月 8 日
This method works as well. Thank you <3

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by