看板 MATLAB 關於我們 聯絡資訊
各位板友大家好 小弟對於num2str和eval寫法有點疑惑,時常跑不出來 譬如我想要求矩陣M1的標準差、M2的標準差...M40的標準差,紀錄在STD的矩陣: STD=[] for r = 1:1:40; caculate = ['STD =[STD, std(M' num2str(r)')]']; eval(caculate) end 試了很多次不同的寫法,不過一直錯誤 請問要怎麼寫才對呢? 另外 num2str的 ' ',要包涵任何文字的地方嗎? 謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 143.167.227.230 ※ 編輯: RachelMcAdam 來自: 143.167.227.230 (03/01 19:45)
profyang:caculate = ['STD =[STD, std(M',num2str(r),')]']; 03/01 23:04
profyang:阿btw...你calculate拼錯了= = 我照你複製也拼錯 不過這 03/01 23:07
profyang:不是重點= = 03/01 23:07
profyang:重點在於 num2str出來的已經是str, 而要把兩個字串串在一 03/01 23:09
profyang:起最直接的寫法就是 [str1,str2]這樣 所以你的形式就應該 03/01 23:09
profyang:要是['blablabla',num2str(r),'blabla']這樣 03/01 23:09
profyang:另外推一個等校的做法: 03/01 23:14
profyang:calculate = sprintf('STD =[STD, std(M%d)]',1); 03/01 23:14
profyang:calculate = sprintf('STD =[STD, std(M%d)]',r); 打錯 03/01 23:14
profyang:這個方法比較不會字串一直被num2str斷開 頗煩的 03/01 23:15
感謝P大解說,sprintf方式感覺好用很多,我來研究看看怎麼用 話說把 caculate = ['STD =[STD, std(M' num2str(r)')]'] 變成 caculate = ['STD =[STD, std(M' num2str(r) ')]'] 就可以跑出來了,真是神秘orz
peterwu76:是不是末端的是num2str,所以前端也要一起num2str 03/02 17:14
peterwu76:然後再eval(字串)? 03/02 17:15
※ 編輯: RachelMcAdam 來自: 31.205.2.248 (03/02 21:11)
profyang:那只是你把空格取代了,的功能而已 03/03 02:21