Can MATLAB create a column of words based on integer values in another column?
조회 수: 1 (최근 30일)
이전 댓글 표시
I’m parsing out columns of data from several text files. One of the columns of data pertains to sub-assembly IDs with a possible range of values between 2 and 103. Depending on the size of the text files, many of these values could easily be repeated numerous times. It’s also worth noting that not all of the known sub-assembly IDs are present in each text file, and they are not always in numerical order. While the nomenclature of each sub-assembly ID is known, they were never included in the text files.
The known sub-assembly ID values are 2, 3, 6, 8, 9, 10, 14, 22, 23, 24, 39, 42, 43, 44, 47, 48, 100, 101, 102, and 103.
A sample of one of these this would be: Sub_ID = [2; 3; 3; 9; 2; 6; 23; 23; 23; 42; 100; 8; 42; 43; 43; 8];
Is there a way I can tell MATLAB to automatically create a column of words (the nomenclature in this case) for each and every one of the Sub_ID values?
If successful, the result would look like this:
2 Sub_ID2
3 Sub_ID3
3 Sub_ID3
9 Sub_ID9
2 Sub_ID2
6 Sub_ID6
23 Sub_ID23
23 Sub_ID23
23 Sub_ID23
42 Sub_ID42
100 Sub_ID100
8 Sub_ID8
42 Sub_ID42
43 Sub_ID43
43 Sub_ID43
8 Sub_ID8
Any ideas on how to approach this are greatly appreciated.
Thank you.
댓글 수: 0
채택된 답변
Azzi Abdelmalek
2013년 10월 10일
Sub_ID = [2; 3; 3; 9; 2; 6; 23; 23; 23; 42; 100; 8; 42; 43; 43; 8];
for k=1:numel(Sub_ID)
out{k,1}=sprintf('Sub_ID%d',Sub_ID(k))
end
댓글 수: 2
Jan
2013년 10월 11일
@Brad: Of course a pre-allocation is essential as usual. So do not forget to initialize the array out by:
out = cell(numel(Sub_ID), 1);
추가 답변 (2개)
Jos (10584)
2013년 10월 10일
Take a look at sprintf and arrayfun :
IDvalues = [1 3 100 12]
IDnames = arrayfun(@(x) sprintf('Sub_ID%d',x), IDvalues, 'un',0)
IDnames is a cell array.
댓글 수: 0
Jan
2013년 10월 11일
편집: Jan
2013년 10월 11일
And a third idea:
Str = sprintf('Sub_ID%d*', Sub_ID);
out = regexp(Str(1:end-1), '*', 'split');
But "the result would look like this" could mean something completely different, perhaps:
Sub_ID = [2; 3; 3; 9; 2; 6; 23; 23; 23; 42; 100; 8; 42; 43; 43; 8] .';
fprintf('% Sub_ID%d\n', cat(1, Sub_ID, Sub_ID))
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!