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
Andrei Bobrov
Andrei Bobrov 2015년 5월 6일
편집: Andrei Bobrov 2015년 5월 6일
Corrected. You are using an old version of MATLAB.

댓글을 달려면 로그인하십시오.


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