在向量中分割大小不连续的矩阵 [英] Splitting non-continuous sized matrix in vectors

查看:83
本文介绍了在向量中分割大小不连续的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Matlab中编写一个软件.用户可以在此处定义尺寸,例如3.

I'm writing an piece of software within Matlab. Here, the user can define a dimension say 3.

此维随后是for循环的迭代次数.在此循环中,我构造了一个矩阵来存储每次迭代期间生成的结果.因此,每次迭代的数据都存储在矩阵的一行中.

This dimension is subsequently the number of iterations of a for loop. Within this loop, I construct a matrix to store the results which are generated during every iteration. So, the data of every iteration is stored in a row of a matrix.

因此,矩阵的大小取决于循环的大小以及用户的输入.

Therefore, the size of the matrix depends on the size of the loop and thus the user input.

现在,我想分隔此矩阵(cl_matrix)的每一行,并自动为每一行创建单独的向量.怎么会这样呢?我被困在这里...

Now, I want to separate each row of this matrix (cl_matrix) and create separate vectors for every row automatically. How would one go on about? I am stuck here...

到目前为止,我有:

Angle = [1 7 15];
for i = 1:length(Angle)
    %% do some calculations here %%
    cl_matrix(i,:) = A.data(:,7);
end

我想根据Angle的长度自动执行此操作:

I want to automate this based on the length of Angle:

length(Angle)
cl_1 = cl_matrix(1,:);
cl_7 = cl_matrix(2,:);
cl_15= cl_matrix(3,:);

谢谢!

推荐答案

workspace变量中动态生成变量whos名称的唯一方法是通过聚集字符串和数值(如您的问题)来构建eval功能.

The only way to dynamically generate in the workspace variables variables whos name is built by aggregating string and numeric values (as in your question) is to use the eval function.

尽管如此,eval只是远离邪恶"的一个字符,既诱人又危险.

Nevertheless, eval is only one character far from "evil", seductive as it is and dangerous as it is as well.

直接使用cl_matrix和生成数组cl_1cl_7cl_15的集合之间可能的折衷可能是创建structure,而fields

A possible compromise between directly working with the cl_matrix and generating the set of array cl_1, cl_7 and cl_15 could be creating a structure whos fields are dynamically generated.

您实际上可以这样生成struct whos字段为cl_1cl_7cl_15:

You can actually generate a struct whos field are cl_1, cl_7 and cl_15 this way:

cl_struct.(['cl_' num2str(Angle(i))])=cl_matrix(i,:)

(您可能会注意到字段名称,例如cl_1,其生成方式与使用eval生成字段的方式相同).

(you might notice the field name, e. g. cl_1, is generated in the same way you could generate it by using eval).

使用eval相对于数组生成,使用此方法具有显着优势:即使不知道其名称,也可以访问fieldstruct(即它们的内容).

Using this approach offers a remarkable advantage with respect to the generation of the arrays by using eval: you can access to the field od the struct (that is to their content) even not knowing their names.

在下面,您可以找到已在其中实施此方法的脚本的修改版本.

In the following you can find a modified version of your script in which this approach has been implemented.

该脚本生成两个结构:

  • 第一个cl_struct_same_length用于存储cl_matrix
  • 的行
  • 第二个,cl_struct_different_length用于存储不同长度的数组
  • the first one, cl_struct_same_length is used to store the rows of the cl_matrix
  • thesecond one, cl_struct_different_length is used to store arrays of different length

在脚本中,有一些示例,说明如何访问文件(即数组)以执行一些计算(在示例中,对每个字段的mean进行评估).

In the script there are examples on how to access to the fileds (that is the arrays) to perform some calculations (in the example, to evaluate the mean of each of then).

您可以使用以下功能访问struct字段:

You can access to the struct fields by using the functions:

  • getfield获取存储在其中的值
  • fieldnames获取字段的名称(动态生成)
  • getfield to get the values stored in it
  • fieldnames to get the names (dynamically generated) of the field

更新的脚本

Angle = [1 7 15];
for i = 1:length(Angle)
   % do some calculations here %%
% % %   cl_matrix(i,:) = A.data(:,7);
   % Populate cl_matrix
    cl_matrix(i,:) = randi(10,1,10)*Angle(i);
    % Create a struct with dinamic filed names
    cl_struct_same_length.(['cl_' num2str(Angle(i))])=cl_matrix(i,:)
    cl_struct_different_length.(['cl_' num2str(Angle(i))])=randi(10,1,Angle(i))
end
% Use "fieldnames" to get the names of the dinamically generated struct's field
cl_fields=fieldnames(cl_struct_same_length)
% Loop through the struct's fileds to perform some calculation on the
% stored values
for i=1:length(cl_fields)
   cl_means(i)=mean(cl_struct_same_length.(cl_fields{i}))
end
% Assign the value stored in a struct's field to a variable
row_2_of_cl_matrix=getfield(cl_struct_different_length,(['cl_' num2str(Angle(2))]))

希望这会有所帮助.

这篇关于在向量中分割大小不连续的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆