作者celestialgod (天)
看板MATLAB
標題Re: [問題] 陣列存值問題
時間Thu Aug 13 18:28:34 2015
% method 1 - for loop
numx = rand(10, 9);
numy = rand(10, 9);
out = struct('numx', [], 'numy', []);
for i = 1:10
for j = 1:9
out(i,j).numx = numx(i,j);
out(i,j).numx = numy(i,j);
end
end
out(5,6).numx
% method 2 - arrayfun
numx = rand(10, 9);
numy = rand(10, 9);
out = arrayfun(@(x, y) struct('numx', x, 'numy', y), numx, numy);
out(5,6).numx
※ 引述《Fugacious (Fugacious)》之銘言:
: 目前有兩個變數空間,內各自有9*10的值
: 假設名字為 numx numy
: 想要做一個陣列Newstruct把 numx 跟 numy 存進去
: 本來是想用for一個一個存
: 使得Newstruct(5,6).numx = numx(5,6)
: 但是結果不如預期
: for i = 1:10
: for j = 1:9
: Newstruct.numx(j,i) = numx(j,i);
: end
: end
: 出來之後的 Newstruct 變成一個有numx numy的東西
: 如果輸入Newstruct(5,6).numx
: 就會顯示超界
: 請問我要怎麼修改程式呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.205.27.107
※ 文章網址: https://www.ptt.cc/bbs/MATLAB/M.1439461717.A.48C.html
推 Fugacious: @是? 08/13 18:40
inline function
推 Fugacious: 如果只執行 for loop 似乎也work? 08/13 19:01
→ Fugacious: 那後面的 arrayfun 是>? 08/13 19:01
兩種方法
※ 編輯: celestialgod (123.205.27.107), 08/13/2015 19:42:32
推 JamesChen: 後面那招很厲害 受教了 08/13 23:13
→ at01076172: 第二招學習了(筆記 08/14 03:17