[英]How to write a matlab code for a given algorithm from a given array and function?
我想編寫一個 matlab 代碼來計算集合 MV = {(1,3) 中的每對 (i,j) 的兩個變量 g(i,j) = i + j + 1 的 function 的值, (2,4), (5,6), (5,4), (7,2)},因此 output 根據以下算法得出 g = {5, 7, 12, 10, 9}:
Step 0. MV_0 = empty set;
Step 1. h=1;
Step 2. while MV_h ~= empty set {
Step 3. for every (i,j) in MV {
Step 4. g(i,j)
Step 5. }
Step 6. h=h++
Step 7. }
到目前為止,我已經嘗試了以下方法,但我無法弄清楚。 請任何提示/幫助。 提前致謝!
MV = {}; % Step 0
h = 1; % Step 1
% MV= intersect(r(r==1),s(s==3))
while isempty(MV{h})==0 % MV{h} is nonempty from Step 2
MV = {[1,3], [2,4], [5,6], [5,4], [7,2]}
% Step 3, for every (i,j) in EMV{h}
for i=1:length(MV)
for j = 1:length(MV)
MV{h} = g(i,j); % Step 4
end
end
h = h+1; % Step 6
end
g % to get the final result g = {5, 7, 12, 10, 9}
% subfunction
function y = g(i,j)
y = i+j+1;
end
我想你需要以下內容:
function Camp()
clear,clc
MV = {};
h = 1;
while isempty(MV)
MV = {[1,3], [2,4], [5,6], [5,4], [6,2]};
for m = 1:length(MV)
i = MV{m}(1);
j = MV{m}(2);
MV{h} = g(i,j)
h = h+1;
end
end
end
% sub function
function y = g(i,j)
y = i+j+1;
end
Output 是:
MV =
Columns 1 through 3
[5] [7] [12]
Columns 4 through 5
[10] [9]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.