在MATLAB中填充矩阵单元格

时间:2016-05-24 19:10:55

标签: matlab

在Matlab中我试图创建一个大小为16 x1的单元格,其中此单元格的每个条目都是一个矩阵。我有以下等式

  $$W_g = exp^{\frac{j{2\pi m}{N}(n+\frac{g}{G}))}  \,\,\,\,\,\,\, m,n=0,1,.....(N-1)$$

这项工作假设$ N = 4 $而索引$ g $是引用单元格元素的索引,即g = 0:1:15

W=cell(16,1);

for g=1:16
for m=1:3
    for n=1:3
        W{g,m,n}= exp((2*pi*j*m/4)*      n+(g-1)/16));
end 
end
end

我该如何使这项工作?我有两个问题,你看到g从0开始,MATLAB不接受零索引以及如何在单元格内实际定义矩阵。

由于

2 个答案:

答案 0 :(得分:0)

所以,如果我理解你有这个等式:

enter image description here

您只需要以下代码:

W=cell(16,1);

n = 1:3;
m = 1:3;
N = 4;
for g=1:16
        W{g}= exp((2*pi*j.*m/4*N).*n+(g-1)/16);
end

%or the 1 line version:

W = cellfun(@(g) exp((2*pi*j.*m/4*N).*n+(g-1)/16),num2cell([1:16]),'UniformOutput',0);

使用matlab,您可以使用Element-wise multiplication符号.*

例如:

%A matrix multiplication
A = [2,3]
B = [1,3]';

result = A * B %result = 11

%An element wise multiplication
A = [2,3]
B = [1,3];

result = A .* B %result = [2,9]

答案 1 :(得分:-1)

首先,我是matlab中的复数(sqrt(-1))而不是j,而且你是正确的,matlab的索引是1,所以只需从1开始计算g,直到16。

接下来,创建一个零矩阵,并相应地计算所有索引。像这样的东西应该可以正常工作:

<dependencyManagement>
        <dependencies>
        <dependency>
            <groupId>com.wordnik</groupId>
            <artifactId>swagger-core_2.10</artifactId>
            <version>1.3.11</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.ws.rs</groupId>
                    <artifactId>jsr311-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson1</artifactId>
            <version>2.22.2</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>${jetty.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>${jetty.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-bundle</artifactId>
            <type>pom</type>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.socrata</groupId>
            <artifactId>soda-api-java</artifactId>
            <version>0.9.12</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>FoodTrucks</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.heroku.sdk</groupId>
                <artifactId>heroku-maven-plugin</artifactId>
                <version>1.0.3</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeScope>compile</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty.version}</version>
                <configuration>
                    <contextPath>/</contextPath>
                    <webApp>
                        <contextPath>/</contextPath>
                        <webInfIncludeJarPattern>.*/.*jersey-[^/]\.jar$</webInfIncludeJarPattern>
                    </webApp>
                    <war>${project.build.directory}/${project.build.finalName}.war</war>
                </configuration>
            </plugin>
        </plugins>
    </build>

如果您正在考虑进行更大规模的操作,请考虑使用linspace来创建m和n索引并使用矩阵运算