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

1 回表示 (過去 30 日間)
Rengin
Rengin 2015 年 5 月 6 日
編集済み: Andrei Bobrov 2015 年 5 月 6 日
% 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 Center および File ExchangeLinear Algebra についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by