Get Column Name if matrix row value is 1

2 Ansichten (letzte 30 Tage)
joms
joms am 3 Jun. 2019
Kommentiert: joms am 5 Jun. 2019
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 Kommentare
joms
joms am 4 Jun. 2019
Bearbeitet: joms am 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 am 4 Jun. 2019
Bearbeitet: joms am 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 '}

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 4 Jun. 2019
Bearbeitet: Stephen23 am 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 Kommentar
joms
joms am 5 Jun. 2019
Thank you very much this works exactly the way i want

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by