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

1 view (last 30 days)
% 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!

Answers (2)

Andrei Bobrov
Andrei Bobrov on 6 May 2015
Edited: Andrei Bobrov on 6 May 2015
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 Comments
Purushottama Rao
Purushottama Rao on 6 May 2015
@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

Sign in to comment.


Purushottama Rao
Purushottama Rao on 6 May 2015
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..

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!