将矩阵的行转换为向量 [英] Convert rows of a matrix to vectors

查看:159
本文介绍了将矩阵的行转换为向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵可以说:

A=[1 2 1; 5 6 7; 7 8 9]

,我想以以下格式提取其行:

and I want to extract its rows in the following format:

x_1=[1 2 1] x_2=[5 6 7] x_3=[7 8 9]

我想知道如何写x_1x_2x_3.我知道如何提取行,但不知道如何创建x_1x_2x_3. 我希望它是自动的,因为我的真实矩阵尺寸很大,并且我不想手工制作x_1 x_2 .. x_100.

I want to know how I can write x_1 , x_2, x_3. I know how to extract the rows but I don't know how to make my x_1, x_2 and x_3. I want this to be automatic, because my real matrix has a very large size and I don't want to make x_1 x_2 .. x_100 by hand.

推荐答案

您可以尝试以下操作:

m = size(A,1);

for i=1:m
    % set the variable name
    varName = sprintf('x_%d',i);

    % create and assign the variable in the base workspace
    assignin('base',varName,A(i,:));
end 

代码遍历 A 的每一行,创建变量名(按照您的格式),然后在MATLAB基础工作区("base")中分配变量,其数据为第i个. A 的行.

The code iterates through every row of A, creates the variable name (as per your format) and then assigns a variable in the MATLAB base workspace ('base') with its data being the ith row of A.

如果通过函数执行此操作,而不是使用"base",请使用"caller"来指示应在函数的工作空间中创建变量.

If doing this from a function, rather than using 'base' use 'caller' to indicate that the variables should be created in the workspace of the function.

这篇关于将矩阵的行转换为向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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