A是一个mXn矩阵,如果我查询v=A(id(j),:),ind是一个jX1为什么Matlab给我v=A(id(j+;1),:)?

A是一个mXn矩阵,如果我查询v=A(id(j),:),ind是一个jX1为什么Matlab给我v=A(id(j+;1),:)?,matlab,matrix,indexing,Matlab,Matrix,Indexing,我有一个文件,其中包含记录了6个实验的数据,记录了这些信息“time ax ay az gx gy gz” 在我的具体案例中,我将其称为logmpu6050,即26220x7矩阵 我可以识别每一个实验,因为时间从上一个较低的随机值重新开始。 因此,当满足该条件时,以下实验的数据从i+1行开始 我定义了一个包含所有这些值的“边界向量”“ind”,并添加了logmpu6050矩阵第一列的第一个(1,1)和最后一个值(end,1),因为这是两个不满足条件的例外 但当我想知道,例如,以下信息: quer

我有一个文件,其中包含记录了6个实验的数据,记录了这些信息“time ax ay az gx gy gz”

在我的具体案例中,我将其称为logmpu6050,即26220x7矩阵

我可以识别每一个实验,因为时间从上一个较低的随机值重新开始。 因此,当满足该条件时,以下实验的数据从i+1行开始

我定义了一个包含所有这些值的“边界向量”“ind”,并添加了logmpu6050矩阵第一列的第一个(1,1)和最后一个值(end,1),因为这是两个不满足条件的例外

但当我想知道,例如,以下信息:

query1=logmpu6050(ind(1),:)
Matlab给了我ind的第二行的值,而不是第一行的值,如附件中的图片所示。为什么?

我还认为它可以从0开始计数,但它是错误的,Matlab用0值显示错误消息

谢谢你,一直以来,我的土木工程师背景让我很难解决这类问题

这里是我写的代码

%Open the file 
filename= uigetfile ('.txt');
fileID = fopen (filename);
logmpu6050 =csvread(filename);
fclose (fileID); 
n=length(logmpu6050);

%Count every time i>i+1 and store the entire raw value

ind=find(diff(logmpu6050(:,1))<0);  
ind=[logmpu6050(1,1);ind(:,:);logmpu6050(end,1)];
%No errors appear - logmpu6050 is a 26220X7 double - ind is a 7x1
ind
query1=logmpu6050(ind(1),:)
query2=logmpu6050(ind(2),:)
%打开该文件
filename=uigetfile('.txt');
fileID=fopen(文件名);
logmpu6050=csvread(文件名);
fclose(fileID);
n=长度(logmpu6050);
%每次i>i+1时计数并存储整个原始值

ind=find(diff(logmpu6050(:,1))我认为查找函数可以很好地提取所需的子矩阵

这里有一个例子

% Generate an example data matrix
n = 3;  % Points per experiment
N = 3;  % Num of experiments
logmpu6050 = [repmat((1:n),1,N); repmat((1:n*N),6,1)]';

% Make a lookup function
lookup = @(x) cumsum([1;diff(logmpu6050(:,1))<0])==x;

% Get experiment 1 data
logmpu6050(lookup(1),:)

% Get experiment 2 data
logmpu6050(lookup(2),:)
第二点:

ans =

     1     4     4     4     4     4     4
     2     5     5     5     5     5     5
     3     6     6     6     6     6     6

另一种方法可以将矩阵拆分为子矩阵的单元阵列:

% Generate an example
N = 3;  % Num of experiments
n = randi([2,5],N,1);  % Points per experiment
logmpu6050 = cell2mat(arrayfun(@(x) [(1:n(x))' x*ones(n(x),6)],1:N,'UniformOutput',0)');

% Find cut points
cuts = [diff(logmpu6050(:,1))<0;1];

% Split into a cell array
experiments = mat2cell(logmpu6050,diff([0;find(cuts)]));

你永远不会在
ind
中添加1。看看
A=diff([12 1])
。你的新实验从索引
3
开始,你当前的
find
逻辑将给你
2
@excaza是的,但要解决这个问题
ind=find(diff(logmpu6050(:,1))这是如何解决问题的?@excaza我想我可以连接我的ind矩阵,将
logmpu6050(1,1)
的第一个值与find矩阵的结果相加
ind=find(diff(logmpu6050(:,1))@excaza我是个花花公子,我终于明白了,我的错误,谢谢你的耐心!)谢谢,我将尝试@Teddy Ort,我想我对在Matlab中建立索引仍然不是很有信心,我研究了文档,高级文档,也许我必须进行更多练习并耐心创建一个包含所有子矩阵(而不是原始矩阵)的循环,如
for ii=1:…-1 expcellmatrix(ii)=logmpu6050(查找(ii),:)结束
?我该怎么做?:)谢谢!很少需要在MATLAB中使用循环。我将发布另一个答案,展示如何将子矩阵拆分为单元数组
% Generate an example
N = 3;  % Num of experiments
n = randi([2,5],N,1);  % Points per experiment
logmpu6050 = cell2mat(arrayfun(@(x) [(1:n(x))' x*ones(n(x),6)],1:N,'UniformOutput',0)');

% Find cut points
cuts = [diff(logmpu6050(:,1))<0;1];

% Split into a cell array
experiments = mat2cell(logmpu6050,diff([0;find(cuts)]));
% The first experiment
experiments{1}

% The second experiment
experiments{2}