Combining text with non-zero elements of a 2D array

1 Ansicht (letzte 30 Tage)
Saeid
Saeid am 16 Sep. 2024
Kommentiert: Saeid am 16 Sep. 2024
I have a numerical array that is "almost" diagonal, so it looks like this:
N=[10 0 0 0 0; 0 20 0 0 0; 10 0 20 0 0; 0 0 0 10 0; 0 0 0 0 30]
N = 5×5
10 0 0 0 0 0 20 0 0 0 10 0 20 0 0 0 0 0 10 0 0 0 0 0 30
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I also have a cell array with the same number of rows, which looks like this:
C={'ABC';'DEF';'GHI';'JKL';'MNO'}
C = 5x1 cell array
{'ABC'} {'DEF'} {'GHI'} {'JKL'} {'MNO'}
I would like to create a row array that takes the non-zero values of N, and combines them with the text in C to give an output like this:
CN={'ABC10_GHI10' 'DEF20' 'GHI10' 'JKL10' 'MNO30'}
CN = 1x5 cell array
{'ABC10_GHI10'} {'DEF20'} {'GHI10'} {'JKL10'} {'MNO30'}
In other words, it must combine all the non-zero values of each column with text of respective indices.

Akzeptierte Antwort

Stephen23
Stephen23 am 16 Sep. 2024
Without an intermediate matrix:
N = [10,0,0,0,0;,0,20,0,0,0;,10,0,20,0,0;,0,0,0,10,0;,0,0,0,0,30]
N = 5×5
10 0 0 0 0 0 20 0 0 0 10 0 20 0 0 0 0 0 10 0 0 0 0 0 30
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C = {'ABC';'DEF';'GHI';'JKL';'MNO'}
C = 5x1 cell array
{'ABC'} {'DEF'} {'GHI'} {'JKL'} {'MNO'}
[X,Y,V] = find(N);
F = @(x)cellstr(join(""+C(X(x))+V(x),'_'));
Z = 1:numel(X);
CN = accumarray(Y,Z(:),[],F)
CN = 5x1 cell array
{'ABC10_GHI10'} {'DEF20' } {'GHI20' } {'JKL10' } {'MNO30' }

Weitere Antworten (2)

Voss
Voss am 16 Sep. 2024
Bearbeitet: Voss am 16 Sep. 2024
Here's one way:
N=[10 0 0 0 0; 0 20 0 0 0; 10 0 20 0 0; 0 0 0 10 0; 0 0 0 0 30]
N = 5×5
10 0 0 0 0 0 20 0 0 0 10 0 20 0 0 0 0 0 10 0 0 0 0 0 30
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C={'ABC';'DEF';'GHI';'JKL';'MNO'}
C = 5x1 cell array
{'ABC'} {'DEF'} {'GHI'} {'JKL'} {'MNO'}
n_col = size(N,2);
tmp = compose('%s%d',string(C),N)
tmp = 5x5 cell array
{'ABC10'} {'ABC0' } {'ABC0' } {'ABC0' } {'ABC0' } {'DEF0' } {'DEF20'} {'DEF0' } {'DEF0' } {'DEF0' } {'GHI10'} {'GHI0' } {'GHI20'} {'GHI0' } {'GHI0' } {'JKL0' } {'JKL0' } {'JKL0' } {'JKL10'} {'JKL0' } {'MNO0' } {'MNO0' } {'MNO0' } {'MNO0' } {'MNO30'}
CN = cell(1,n_col);
idx = N ~= 0;
for ii = 1:n_col
CN{ii} = strjoin(tmp(idx(:,ii),ii),'_');
end
disp(CN)
{'ABC10_GHI10'} {'DEF20'} {'GHI20'} {'JKL10'} {'MNO30'}

Voss
Voss am 16 Sep. 2024
Bearbeitet: Voss am 16 Sep. 2024
Here's one way:
N=[10 0 0 0 0; 0 20 0 0 0; 10 0 20 0 0; 0 0 0 10 0; 0 0 0 0 30]
N = 5×5
10 0 0 0 0 0 20 0 0 0 10 0 20 0 0 0 0 0 10 0 0 0 0 0 30
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C={'ABC';'DEF';'GHI';'JKL';'MNO'}
C = 5x1 cell array
{'ABC'} {'DEF'} {'GHI'} {'JKL'} {'MNO'}
n_col = size(N,2);
tmp = compose("%s%d",string(C),N)
tmp = 5x5 string array
"ABC10" "ABC0" "ABC0" "ABC0" "ABC0" "DEF0" "DEF20" "DEF0" "DEF0" "DEF0" "GHI10" "GHI0" "GHI20" "GHI0" "GHI0" "JKL0" "JKL0" "JKL0" "JKL10" "JKL0" "MNO0" "MNO0" "MNO0" "MNO0" "MNO30"
CN = strings(1,n_col);
idx = N ~= 0;
for ii = 1:n_col
CN(ii) = join(tmp(idx(:,ii),ii),'_');
end
disp(CN)
"ABC10_GHI10" "DEF20" "GHI20" "JKL10" "MNO30"
CN = cellstr(CN)
CN = 1x5 cell array
{'ABC10_GHI10'} {'DEF20'} {'GHI20'} {'JKL10'} {'MNO30'}

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by