如何在matlab中索引数组以获得合适的矩阵

时间:2014-04-03 06:38:26

标签: matlab

我是matlab的新手并尝试这样做:

>>matdup(1,2,2)
ans = 1 1

      1 1 

但我的代码输出不同:

function m = matdup( input,row,col )
%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here
    for i = 1:row
        for j = 1:col
            m{i, j}= input;
        end
    end

end

但这就是给我这样的

[1] [1]
[1] [1]

???任何想法如何索引properply ???因为这显示了每个单独的矩阵

1 个答案:

答案 0 :(得分:1)

使用()(数组数据索引)而不是{}(单元数据索引)

function m = matdup( input,row,col )
%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here
    for i = 1:row
        for j = 1:col
            m(i, j)= input;
        end
    end

end

但您也可以使用:

>> ones(2)

ans =

     1     1
     1     1

>> ones(row,col).*input 

复制你想要的行为。

>> ones(3,2).*5

ans =

     5     5
     5     5
     5     5