如何在MATLAB中通过for循环检索行

时间:2016-02-02 21:39:59

标签: matlab for-loop matrix

[4*x1*f1   5*f2;    10*f1/x2     f2*x1*x2]

我有一个2x2矩阵,但我需要单独检索每一行。我如何循环它以便MATLAB可以找到矩阵的长度,并保持循环打开,直到索引等于所述矩阵中的行数?似乎MATLAB函数是预先打包设计的,用于遍历行,但我需要为自己编写一个自定义函数。

1 个答案:

答案 0 :(得分:0)

I think you are interested in the syntax A(1,:) to get row 1, etc.

A = [4*x1*f1   5*f2;    10*f1/x2     f2*x1*x2];
[r,~] = size(A);  % get the number of rows, save as 'r'
for i = 1:r  %% loop over the number of rows
    row = A(i,:);  %% save the individual row
    %% my_func(row)   <<-- put whatever code you want here
end