How can I create a sub-matrix if the number of rows change according to main-matrix?

1 次查看(过去 30 天)
% I have 9x1 sized A matrix as below:
A=[2.36; 2.36; 2.36; 1.93; 1.93; 1.41; 1.41; 1.41; 0.39]
% from 3rd row to 4th row
% from 5th row to 6th row
% from 8th row to 9th row, values of rows change
% I wanna have B=[4;6;9]. it is just an example! I wanna have a general code including the value changes based on from ith row to jth row...In the end B matrix could be like B= [j;...;....] (maybe 15x1 sized matrix)
P.S : The number of columns is always "1"
% How can I solve that problem? Many Thanks!

回答(2 个)

Andrei Bobrov
Andrei Bobrov 2015-5-6
编辑:Andrei Bobrov 2015-5-6
A=[2.36; 2.36; 2.36; 1.93; 1.93; 1.41; 1.41; 1.41; 0.39];
[a,b,c] = unique(A,'first');
[ignored,ii] = sort(b);
b1 = b(ii);
a1 = a(ii);
B = b1(2:end);
out = diff(a1);
or
D = diff(A(:));
B = find([false;D~=0]);
out = D(B - 1);
  2 个评论
Purushottama Rao
Purushottama Rao 2015-5-6
@Andrei: Sir,I tried your code and got the following error meesage
A=[2.36; 2.36; 2.36; 1.93; 1.93; 1.41; 1.41; 1.41; 0.39]; [a,b,c] = unique(A,'first'); [~,ii] = sort(b); b1 = b(ii); a1 = a(ii); B = b1(2:end); out = diff(a1); ??? [~,ii] = sort(b); | Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
what was that

请先登录,再进行评论。


Purushottama Rao
Purushottama Rao 2015-5-6
clc
k=0;
for i=2:(length(A))
if (A(i)~=A(i-1))
k=k+1
b(k)=i
end
end
Try out this..

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by