一千萬個為什麽

搜索

在Matlab程序上循環

我每組有3組10個向量,我想從第一組中取出2個向量,從第二組中取出2個向量,從第三組中取出3個向量。我的目標是創建一個循環來實現以下程序,知道每次計算後,結果應保存在一個新文件中。
我的問題是我無法處理字符串中包含的索引。我嘗試使用多個循環按索引的順序掃描3組。循環應包含以下程序

clc;  
clear all;  

load('C:\Users\Documents\MATLAB\myFile\matrice_F.mat');  
load('C:\Users\Documents\MATLAB\myFile\matrice_G.mat');  

F = m_F;  
G = m_G;  


load('C:\Users\Documents\MATLAB\myFile\matrice_J.mat');  
load('C:\Users\Documents\MATLAB\myFile\matrice_K.mat');  

J = m_J;  
K = m_K;  

load('C:\Users\Documents\MATLAB\myFile\matrice_N.mat');  
load('C:\Users\Documents\MATLAB\myFile\matrice_O.mat');  
load('C:\Users\Documents\MATLAB\myFile\matrice_P.mat');    
N = m_N ;  
O = m_O;  
P = m_P;  

[A,B,C,D,E] = myFun(F,G,J,K,N,O,P);  

file_name = 'matrice_final.mat';  
save(file_name,'A','B','C','D','E');   

我感謝所有回答我問題的人。對不起,如果我從一開始就不那麽準確。但是,我希望該程序看起來像:

clc; 


    clear all;    

    set1={F,G,FF,GG,X,Y,XX,L,BH,JK};  %set of name vectors  
    set2={J,K,HG,UY,TR,BC,XW,IOP,ES,QA};  %set of name vectors  
    set3={AJ,RK,DS,TU,WS,ZZE,ZXW,TYP,ZAA,QWW};  %set of name vectors  

for i=1:1:9    

        load('C:\Users\Documents\MATLAB\myFile\matrice_'set1(i)'.mat');    
        load('C:\Users\Documents\MATLAB\myFile\matrice_'set1(i+1)'.mat');   

        'set1(i)' = m_'set1(i)';     
        'set1(i+1)' = m_'set1(i+1)';     

    for j=1:1:9   

        load('C:\Users\Documents\MATLAB\myFile\matrice_'set2(j)'.mat');     
        load('C:\Users\Documents\MATLAB\myFile\matrice_'set2(j+1)'.mat');  

        'set2(j)' = m_'set2(j)';     
        'set2(j+1)' = m_'set2(j+1)';   

        for k=1:1:8   

            load('C:\Users\Documents\MATLAB\myFile\matrice_'set3(k)'.mat');       
            load('C:\Users\Documents\MATLAB\myFile\matrice_'set3(k+1)'.mat');      
            load('C:\Users\Documents\MATLAB\myFile\matrice_'set3(k+2)'.mat');        

            'set3(k)' = m_'set3(k)' ;      
            'set3(k+1)' = m_'set3(k+1)';     
            'set3(k+2)' = m_'set3(k+2)';      

    [Result1'index',Result2'index',Result3'index',Result4'index',Result5'index'] = myFun('set1(i)','set1(i+1)','set2(j)','set2(j+1)','set3(k)','set3(k+1)','set3(k+2)');  %% 9x9x8=648   index=1,2,...,648      

    file_name = 'matrice_final'index'.mat';      
    save(file_name,'Result1'index'','Result2'index'','Result3'index'','Result4'index'','Result5'index'');        
        clear 'set3(k)' 'set3(k+1)' 'set3(k+2)'   
        end   
    clear 'set2(j)' 'set2(j+1)'   
    end   
    clear 'set1(i)' 'set1(i+1)'
end

最佳答案

註意:已更新以滿足問題中的新要求...

這是一個避免使用 EVAL 的解決方案:

clc;
clear all;

set1 = {'F','G','FF','GG','X','Y','XX','L','BH','JK'};
set2 = {'J','K','HG','UY','TR','BC','XW','IOP','ES','QA'};
set3 = {'AJ','RK','DS','TU','WS','ZZE','ZXW','TYP','ZAA','QWW'};

matrices = cell(1,7);  %# An empty cell array
fileString = 'C:\Users\Documents\MATLAB\myFile\matrice_';  %# First part of
                                                           %#   file name

for i = 1:9  %# Loop over set1

  fileData = load([fileString set1{i} '.mat']);  %# Load file
  matrices{1} = fileData.(['m_' set1{i}]);       %# Get matrix

  fileData = load([fileString set1{i+1)} '.mat']);  %# Load file
  matrices{2} = fileData.(['m_' set1{i+1}]);        %# Get matrix

  for j = 1:9  %# Loop over set2

    fileData = load([fileString set2{j} '.mat']);  %# Load file
    matrices{3} = fileData.(['m_' set2{j}]);       %# Get matrix

    fileData = load([fileString set2{j+1)} '.mat']);  %# Load file
    matrices{4} = fileData.(['m_' set2{j+1}]);        %# Get matrix

    for k = 1:8  %# Loop over set3

      fileData = load([fileString set3{k} '.mat']);  %# Load file
      matrices{5} = fileData.(['m_' set3{k}]);       %# Get matrix

      fileData = load([fileString set3{k+1} '.mat']);  %# Load file
      matrices{6} = fileData.(['m_' set3{k+1}]);       %# Get matrix

      fileData = load([fileString set3{k+2} '.mat']);  %# Load file
      matrices{7} = fileData.(['m_' set3{k+2}]);       %# Get matrix

      [A,B,C,D,E] = myFun(matrices{:});  %# Pass matrices to your function

      index = k+8*(j-1)+72*(i-1);        %# Combination index
      fileName = ['matrice_final' int2str(index) '.mat'];  %# Save file name
      save(fileName,'A','B','C','D','E');                  %# Save the data

    end
  end
end

我做了一個小修改。而不是通過文件 matrice_final1.mat 中的 E1 保存 A1 ,而不是通過 E2 A2 >在文件 matrice_final2.mat 等中,我只是在每個文件中通過 E 保存了 A ,因為文件名本身已經指示了索引。如果要在沒有矩陣 AE 的情況下一次加載多個文件,可以執行以下操作:

data1 = load('matrice_final1.mat');  %# Load data into structure data1
data2 = load('matrice_final2.mat');  %# Load data into structure data2

現在, data1.A 將為索引1提供 Adata2.A 將為您提供 A 指數2。

轉載註明原文: 在Matlab程序上循環