Adding 0 value place holders to string array

조회 수: 34 (최근 30일)
Lou
Lou 2024년 12월 12일 23:49
편집: Walter Roberson 2024년 12월 13일 3:18
Appreciate any help on this challenge. Ive spent a few hours already and havent figured it out. I have a 2500x1 string array. Lets say the first 5 rows look like this:
"0, 74, 46, 94"
"7, 64, 8"
"8, 50, 32, 70"
"6, 43"
"3, 55, 47"
How can I add zeros as place holders for the rows that have less than 4 values? I want the result to look like this:
"0, 74, 46, 94"
"7, 64, 8, 0"
"8, 50, 32, 70"
"6, 43, 0, 0"
"3, 55, 47, 0"

채택된 답변

Paul
Paul 2024년 12월 13일 1:03
s= [
"0, 74, 46, 94"
"7, 64, 8"
"8, 50, 32, 70"
"6, 43"
"3, 55, 47"]
s = 5x1 string array
"0, 74, 46, 94" "7, 64, 8" "8, 50, 32, 70" "6, 43" "3, 55, 47"
c = count(s,",")
c = 5×1
3 2 3 1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
s = arrayfun(@(s,c) join([s,repmat(", 0",1,3-c)],""),s,c)
s = 5x1 string array
"0, 74, 46, 94" "7, 64, 8, 0" "8, 50, 32, 70" "6, 43, 0, 0" "3, 55, 47, 0"
  댓글 수: 1
Lou
Lou 2024년 12월 13일 3:10
That worked perfectly! Thank you!!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by