add space column to matrix

4 ビュー (過去 30 日間)
Keren Grinberg
Keren Grinberg 2022 年 3 月 6 日
回答済み: DGM 2022 年 3 月 6 日
i need to write *.inp file,
i have 2 matrix that i want to conbine:
A=[1 2 3;4 5 6]
and matrix i created from vectorL
b=['a' 'b' 'c' 'd']
B=repelem(b,2,1)
C=[1 2 3 ;'a' 'b' 'c' 'd';4 5 6 ;'s' 's' 's' 's']
after 3 and 6 i need space string\element.
I did this code:
C is great, but the inp file isnt
1, 2, 3, NaN
NaN, NaN, NaN, NaN
4, 5, 6, NaN
NaN, NaN, NaN, NaN
the most importent thing is that i need spaces charecters instead of 'NaN' in the file
thanks!!
A=[1 2 3;4 5 6]
b=["a", 'b', 'c', 'd'];
B=repelem(b,2,1)
nans=nan(2,1)
a=[A nans];
aB=[a B]'
C=reshape(aB,4,4)'
fileID=fopen('AB.inp','w')
fprintf(fileID,'\n%G, %G, %G, %G',C')
fclose(fileID);
type AB.inp

回答 (1 件)

DGM
DGM 2022 年 3 月 6 日
See if this is more along the lines of what you need
A = [1 2 3;4 5 6]
b = {'a' 'b' 'c' 'd'}
Ac = split(sprintf('%G\n',A')) % convert numeric to cellchar
Ac = reshape(Ac(1:end-1),size(A)) % reshape
Ac = [Ac {' '; ' '}]; % append spaces
C = [Ac(1,:); b; Ac(2,:); b].' % concatenate and transpose
fileID=fopen('AB.inp','w');
fprintf(fileID,'\n%s, %s, %s, %s',C{:});
fclose(fileID);

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by