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

1 vue (au cours des 30 derniers jours)
Rengin
Rengin le 6 Mai 2015
% 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!

Réponses (2)

Andrei Bobrov
Andrei Bobrov le 6 Mai 2015
Modifié(e) : Andrei Bobrov le 6 Mai 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 commentaires
Purushottama Rao
Purushottama Rao le 6 Mai 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
Andrei Bobrov
Andrei Bobrov le 6 Mai 2015
Modifié(e) : Andrei Bobrov le 6 Mai 2015
Corrected. You are using an old version of MATLAB.

Connectez-vous pour commenter.


Purushottama Rao
Purushottama Rao le 6 Mai 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..

Catégories

En savoir plus sur Mathematics and Optimization dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by