Get the Array Number, when value changes

6 ビュー (過去 30 日間)
Marv
Marv 2015 年 7 月 29 日
編集済み: Stephen23 2015 年 10 月 6 日
Hello, I have the following array:
1 Monday
2 Monday
3 Monday
4 Sunday
5 Sunday
6 Friday
7 Friday
8 Friday
...
I would like to get the array number when the value changes. These are the "limits" for the values. Then I want to save these limits in new arrays, in this case:
Limits.Monday:
1
3
Limits.Sunday:
4
5
Limits.Friday
6
8
Thanks :-)
  1 件のコメント
Marv
Marv 2015 年 7 月 29 日
I forgot to mention that there could be several values with the same name, for example:
1 Monday
2 Monday
3 Monday
4 Sunday
5 Sunday
6 Monday
7 Monday
...
Is it possible to get all limits by increasing order ?
Limits.1_Monday
1
3
Limits.2_Sunday
4
5
Limits.3_Monday
6
7

サインインしてコメントする。

回答 (5 件)

Andrei Bobrov
Andrei Bobrov 2015 年 7 月 29 日
編集済み: Andrei Bobrov 2015 年 7 月 29 日
A = {1 'Monday'
2 'Monday'
3 'Monday'
4 'Sunday'
5 'Sunday'
6 'Friday'
7 'Friday'
8 'Friday'};
[a,~,c] = unique(A(:,2),'stable');
Limits = cell2struct(accumarray(c,cell2mat(A(:,1)),[],@(x){[min(x);max(x)]}),a,1);
add variant
A = {1 'Monday'
2 'Monday'
3 'Monday'
4 'Sunday'
5 'Sunday'
6 'Monday'
7 'Monday'
8 'Friday'};
[a,~,c] = unique(A(:,2),'stable');
i1 = [true;diff(c)~=0];
x = accumarray(cumsum(i1),cell2mat(A(:,1)),[],@(x){[min(x);max(x)]});
y = strcat(A(i1,2),{'_'},arrayfun(@(x)sprintf('%d',x),(1:nnz(i1))','un',0));
Limits = cell2struct(x,y,1);
  2 件のコメント
Marv
Marv 2015 年 7 月 29 日
I think this solutions outputs the number in the left column ? But I would like to output the array number, like:
Range_Monday=A(1:3,2)
= A(Limits.1_Monday(1,1),Limits.1_Monday(2,1))
Andrei Bobrov
Andrei Bobrov 2015 年 7 月 29 日
See 'add variant' from my answer.

サインインしてコメントする。


Stephen23
Stephen23 2015 年 7 月 29 日
編集済み: Stephen23 2015 年 7 月 29 日
Try this:
X = {1,'Monday';2,'Monday';3,'Monday';4,'Sunday';5,'Sunday';6,'Monday';7,'Monday'};
Y = [true;any(diff(char(X(:,2)),1,1),2);true];
idx = cumsum(Y(1:end-1));
out(:,2) = accumarray(idx,[X{:,1}],[],@(x){x([1,end])});
out(:,1) = X(Y(1:end-1),2);
and here is the output in the command window:
>> out
out =
'Monday' [2x1 double]
'Sunday' [2x1 double]
'Monday' [2x1 double]
>> out{1,2}
ans =
1
3
>> out{2,2}
ans =
4
5
>> out{3,2}
ans =
6
7
I did not put the data into a structure because while a structure with fields Monday, Tuesday, etc, makes perfect sense, placing indices in front of the names a) is not a valid variable/fieldname in MATLAB, and b) implies that the sequence order is significant, therefore an indexed container (e.g. a cell array) is more suitable.

Marv
Marv 2015 年 10 月 2 日
Hm I still dont have the right solution.
Attached I uploaded you the data I want to analyse.
Every time the value changes, I want to get the array / column number where it changed. The output shall be a range, same as in the example above.
The problem is that there are several values with different ranges, for example: '0' is from column 1 to 57 but also from 5016 to 5110 !
So can you output the ranges chronologically please ? Like 'Range'_ChronologicNr_ArrayValue:
Range_1_0 =
[1]
[57]
Range_2_6.1029 =
[58]
[529]
Range_3_0 =
[530]
[585]
Range_4_13.0064 =
[586]
[852]
Range_5_11.0053 =
[853]
[1535]
Thanks :)
  2 件のコメント
Thorsten
Thorsten 2015 年 10 月 2 日
You forgot to attach the file.
Marv
Marv 2015 年 10 月 3 日
編集済み: Marv 2015 年 10 月 3 日
Sorry, above is the attached file.

サインインしてコメントする。


Marv
Marv 2015 年 10 月 4 日
Can someone help me ? I need a solution today :-/
  1 件のコメント
Stephen23
Stephen23 2015 年 10 月 4 日
編集済み: Stephen23 2015 年 10 月 4 日
The data matrix that you uploaded does not match the question. Your question states "I have the following array:"
1 Monday
2 Monday
3 Monday
4 Sunday
...
but your data file consists of a single vector of type double containing decimal values. This is a completely different format to what you question is asking about. If you want help then you need to provide clear explanations for us, and make sure that your data makes sense.
Do you want us the use your uploaded data, which has nothing to do with your question, or shall we invent some data ourselves?
You should read this, and please put into practice what it advises:

サインインしてコメントする。


Stephen23
Stephen23 2015 年 10 月 4 日
編集済み: Stephen23 2015 年 10 月 5 日
Given your uploaded data (attached below), this code will identify the locations where the value changes:
tol = 0.01;
data = load('Log_data_Changing_Value.mat');
idx = find(diff(data.A)>tol)
and it displays these in the command window:
idx =
57
585
1535
1997
2438
4298
If you explain exactly what you need to do with these indices then we can help you to use them too.
  4 件のコメント
Marv
Marv 2015 年 10 月 5 日
Alright, so I need "data.A(idx)" to export the values ?
Stephen23
Stephen23 2015 年 10 月 6 日
編集済み: Stephen23 2015 年 10 月 6 日
Correct. This is called indexing, and is a basic tool used in MATLAB for accessing data. To learn how to use indexing your should work through some of the tutorials:
And if my Answer has helped you please click on Accept.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by