Get Column Name if matrix row value is 1

3 views (last 30 days)
Im creating a labeling for x axis of a graph that track change in value based on first row of a matrix as basevalue.
I had managed to code until validmatrix array but i dont know how to extract table label if valid index is "1".
validindexarray determines what element changes per row and the "parameterlabel" is the label name of the table. If validndex is 1 then desiredresult will get the parameter label from the table.
Thanks for your help.
parameterlabel={ 'A' 'B' 'C' 'D' 'E'}
parametervalue=[ 1 2 4 5 3; 2 2 4 5 3; 1 2 5 5 3; 1 2 3 5 2]
validindex= [0 0 0 0 0; 1 0 0 0 0; 0 0 1 0 0; 0 0 1 0 1]
%%My code until here↑↑↑↑
%Need help beyond this line↓↓↓
desiredresult={'','A 1→2','C 4→5','C 4→3 ,E 3→2 '}
xticklabels(desiredresult)
xticklabels({'','A 1→2','C 4→5','C 4→3 ,E 3→2'})
  4 Comments
joms
joms on 4 Jun 2019
Edited: joms on 4 Jun 2019
validindexarray determines what element changes per row and the "parameterlabel" is the label name of the column where the value change. If validndex is 1 then desiredresult will get the parameter label from the table. ' 4→3,3→2' are the values that change C , E are columns name where the change occurs
joms
joms on 4 Jun 2019
Edited: joms on 4 Jun 2019
since there are 5 rows C and E will be combined like this
desiredresult={'','A 1→2','C 4→5','C 4→3 ,E 3→2 '}

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 4 Jun 2019
Edited: Stephen23 on 4 Jun 2019
First download Jos's excellent runindex:
Data:
L = {'A','B','C','D','E'};
P = [1 2 4 5 3; 2 2 4 5 3; 1 2 5 5 3; 1 2 3 5 2]
X = [0 0 0 0 0; 1 0 0 0 0; 0 0 1 0 0; 0 0 1 0 1]
Method:
Z = cell2mat(cellfun(@runindex,num2cell(X,1),'uni',0));
[R,C] = find(X);
V = 1:nnz(X);
F = @(r,c)sprintf('%s %d->%d',L{c},P([r-Z(r,c),r],c));
D = arrayfun(F,R,C,'uni',0);
G = @(s)s(3:end);
H = @(d){G(sprintf(', %s',D{d}))};
D = accumarray(R,V(:),[],H);
D(cellfun('isempty',D)) = {''}
Giving:
D =
''
'A 1->2'
'C 4->5'
'C 4->3, E 3->2'
  1 Comment
joms
joms on 5 Jun 2019
Thank you very much this works exactly the way i want

Sign in to comment.

More Answers (0)

Products


Release

R2013b

Community Treasure Hunt

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

Start Hunting!