필터 지우기
필터 지우기

add space column to matrix

조회 수: 7 (최근 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 CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by