How to make a matrix with mixed string and number elements ?

조회 수: 22 (최근 30일)
Zahra
Zahra 2022년 7월 13일
댓글: Jon 2022년 7월 13일
Hi ,
I want to make a Matrix with i rows and 4 colomns. I want to fill it with <X000i> with i a number changing from 1 to N . If the matrix name is A , I want the elemnts to be in the next order :
for i = 1 : N ( if N =10 )
A(1 , 1 ) = <X0001>
A(1 , 2 ) = <X0002>
A(1 , 3 ) = <X0003>
A(1 , 4 ) = <X0004>
A(2 , 1 ) = <X0005>
A(2 , 2 ) = <X0006>
A(2 , 3 ) = <X0007>
A(2 , 4 ) = <X0008>
.............
.............
A(10 , 1 ) = <X0037>
A(10 , 2 ) = <X0038>
A(10 , 3 ) = <X0039>
A(10 , 4 ) = <X0040>
There shoud be only 4 digits after the X .
The hard part is that I don't knwo th code to have eac elemnt of the matrix a micture of number and string.
I woud really appriciate if someone could help me solve my problem .
Thank you .

채택된 답변

Adam Danz
Adam Danz 2022년 7월 13일
편집: Adam Danz 2022년 7월 13일
I think this is your goal
N = 10;
M = 4;
str = compose("x%04d",1:N*M);
A = reshape(str,M,N)'
A = 10×4 string array
"x0001" "x0002" "x0003" "x0004" "x0005" "x0006" "x0007" "x0008" "x0009" "x0010" "x0011" "x0012" "x0013" "x0014" "x0015" "x0016" "x0017" "x0018" "x0019" "x0020" "x0021" "x0022" "x0023" "x0024" "x0025" "x0026" "x0027" "x0028" "x0029" "x0030" "x0031" "x0032" "x0033" "x0034" "x0035" "x0036" "x0037" "x0038" "x0039" "x0040"
Note that this is referred to as a string array rather than a matrix. You could also create a cell array of character vectors:
str = compose('x%04d',1:N*M);
A = reshape(str,M,N)'
A = 10×4 cell array
{'x0001'} {'x0002'} {'x0003'} {'x0004'} {'x0005'} {'x0006'} {'x0007'} {'x0008'} {'x0009'} {'x0010'} {'x0011'} {'x0012'} {'x0013'} {'x0014'} {'x0015'} {'x0016'} {'x0017'} {'x0018'} {'x0019'} {'x0020'} {'x0021'} {'x0022'} {'x0023'} {'x0024'} {'x0025'} {'x0026'} {'x0027'} {'x0028'} {'x0029'} {'x0030'} {'x0031'} {'x0032'} {'x0033'} {'x0034'} {'x0035'} {'x0036'} {'x0037'} {'x0038'} {'x0039'} {'x0040'}

추가 답변 (1개)

Jon
Jon 2022년 7월 13일
Here's another approach, not so nicely vectorized as @Adam Danz, in which I store the values in a cell array
I hadn't known about the compose function when I wrote up the attached answer. Thanks @Adam Danz for teaching me about the compose function with your answer.
numRows = 10;
numColumns = 4;
% cacluate total number of elements
N = numRows*numColumns;
% generate cell array with elements of form <X000i>
A = cell(numRows,numColumns); % preallocate cell array to hold values
for i = 1:numRows
for j = 1:numColumns
k = sub2ind([numRows,numColumns],i,j);% get linear index
A{i,j} = num2str(k,'<%04d>'); % e.g. '<0035>'
end
end
disp(A)
{'<0001>'} {'<0011>'} {'<0021>'} {'<0031>'} {'<0002>'} {'<0012>'} {'<0022>'} {'<0032>'} {'<0003>'} {'<0013>'} {'<0023>'} {'<0033>'} {'<0004>'} {'<0014>'} {'<0024>'} {'<0034>'} {'<0005>'} {'<0015>'} {'<0025>'} {'<0035>'} {'<0006>'} {'<0016>'} {'<0026>'} {'<0036>'} {'<0007>'} {'<0017>'} {'<0027>'} {'<0037>'} {'<0008>'} {'<0018>'} {'<0028>'} {'<0038>'} {'<0009>'} {'<0019>'} {'<0029>'} {'<0039>'} {'<0010>'} {'<0020>'} {'<0030>'} {'<0040>'}
  댓글 수: 1
Jon
Jon 2022년 7월 13일
I guess I forgot that you also wanted the X in your strings, to get this use
A{i,j} = num2str(k,'<X%04d>')
You can also use this in @Adam Danz approach, using the compose function which is nicer because it doesn't need any loops.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by