Reshaping a matrix based on the first row

9 vues (au cours des 30 derniers jours)
Kendall Galvez
Kendall Galvez le 24 Avr 2024
Commenté : Voss le 27 Avr 2024
I am trying to make a for loop that creates a new array that groups row data into corresponding columns:
My current arrays are something like this (both same size):
[270 270 270 271 272 272 273 273 273]
[12 2 3 14 5 2 6 8 11]
I want 4 columns (each representing 270, 271, 272, 273 respectively) that will produce something like this:
[12 14 5 6; 2 NAN 2 8; 3 NAN NAN 11]
my for loop is currently creating a 9 by 3 array that has the correct values in each column but is producing a bigger shape (more NANs).
for i = 1:length(data1)
for j = 1:length(number)
if data1(i) == number(j) % my dataset == a number array [270, 271, 272, 273]
newArray(i,j) = data2(i);
end
end
end

Réponses (2)

Voss
Voss le 24 Avr 2024
Here's one way:
A = [270 270 270 271 272 272 273 273 273];
B = [ 12 2 3 14 5 2 6 8 11];
[~,~,cidx] = unique(A,'stable');
ridx = accumarray(cidx,A,[],@(x){1:numel(x)});
ridx = [ridx{:}].';
NR = max(ridx);
NC = max(cidx);
result = NaN(NR,NC);
result(sub2ind([NR,NC],ridx,cidx)) = B
result = 3x4
12 14 5 6 2 NaN 2 8 3 NaN NaN 11
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  4 commentaires
Kendall Galvez
Kendall Galvez le 26 Avr 2024
A = EventDays
B = Events
Voss
Voss le 27 Avr 2024
A = load('EventDays.mat').doy_e
A = 1x2065
270 270 270 270 270 NaN 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B = load('Events.mat').events
B = 1x2065
15 2 5 4 5 0 0 3 4 9 1 5 5 10 10 11 11 9 7 3 4 6 1 3 6 0 7 0 3 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
idx = ~isnan(A);
[~,~,cidx] = unique(A(idx),'stable');
ridx = accumarray(cidx,A(idx),[],@(x){1:numel(x)});
ridx = [ridx{:}].';
NR = max(ridx);
NC = max(cidx);
result = NaN(NR,NC);
result(sub2ind([NR,NC],ridx,cidx)) = B(idx)
result = 72x31
15 7 1 4 4 3 9 2 1 6 2 5 4 1 1 1 0 2 1 3 3 2 3 3 1 5 2 1 0 0 2 1 3 0 3 4 2 1 6 13 2 3 2 1 0 0 1 0 5 4 16 14 5 25 0 0 0 0 0 0 5 2 9 1 5 1 0 2 3 7 0 0 3 0 0 2 7 2 1 13 1 1 2 5 2 0 0 0 7 0 4 6 4 1 8 1 0 0 0 1 2 3 3 0 0 4 1 8 3 3 2 8 6 2 9 0 5 0 3 0 5 4 8 6 4 1 4 4 4 4 1 1 3 6 9 1 0 3 12 4 7 2 1 10 0 1 1 0 0 3 0 2 1 2 3 2 8 3 14 5 1 3 2 0 0 0 0 2 3 9 4 6 4 3 0 0 0 0 0 0 3 4 4 3 8 5 6 4 3 5 0 3 0 1 5 3 2 8 10 4 7 6 3 5 1 0 0 0 1 0 4 3 2 6 5 5 1 1 10 0 1 1 1 3 8 1 2 1 3 19 7 2 2 4 0 5 2 1 5 1 9 1 2 7 12 2 2 2 1 0 10 7 6 0 0 0 2 1 16 12 0 5 1 1 0 0 1 0 0 1 1 5 0 1 10 1 3 4 6 4 1 3 23 1 3 0 4 9 12 9 15 11 7 0 0 1 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Connectez-vous pour commenter.


Stephen23
Stephen23 le 27 Avr 2024
Modifié(e) : Stephen23 le 27 Avr 2024
A = [270,270,270,271,272,272,273,273,273];
B = [ 12, 2, 3, 14, 5, 2, 6, 8, 11];
X = ~isnan(A);
C = findgroups(A(X));
R = grouptransform(ones(nnz(X),1),C(:),@cumsum);
M = accumarray([R,C(:)],B(X),[],[],NaN)
M = 3x4
12 14 5 6 2 NaN 2 8 3 NaN NaN 11
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 commentaire
Stephen23
Stephen23 le 27 Avr 2024
Using your uploaded data (my code is unchanged). Note that your data also has NaNs in it, which so far you have not explained how you want to handle. I will remove them.
A = load('EventDays.mat').doy_e
A = 1x2065
270 270 270 270 270 NaN 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B = load('Events.mat').events
B = 1x2065
15 2 5 4 5 0 0 3 4 9 1 5 5 10 10 11 11 9 7 3 4 6 1 3 6 0 7 0 3 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
X = ~isnan(A);
C = findgroups(A(X));
R = grouptransform(ones(nnz(X),1),C(:),@cumsum);
M = accumarray([R,C(:)],B(X),[],[],NaN)
M = 72x31
15 7 1 4 4 3 9 2 1 6 2 5 4 1 1 1 0 2 1 3 3 2 3 3 1 5 2 1 0 0 2 1 3 0 3 4 2 1 6 13 2 3 2 1 0 0 1 0 5 4 16 14 5 25 0 0 0 0 0 0 5 2 9 1 5 1 0 2 3 7 0 0 3 0 0 2 7 2 1 13 1 1 2 5 2 0 0 0 7 0 4 6 4 1 8 1 0 0 0 1 2 3 3 0 0 4 1 8 3 3 2 8 6 2 9 0 5 0 3 0 5 4 8 6 4 1 4 4 4 4 1 1 3 6 9 1 0 3 12 4 7 2 1 10 0 1 1 0 0 3 0 2 1 2 3 2 8 3 14 5 1 3 2 0 0 0 0 2 3 9 4 6 4 3 0 0 0 0 0 0 3 4 4 3 8 5 6 4 3 5 0 3 0 1 5 3 2 8 10 4 7 6 3 5 1 0 0 0 1 0 4 3 2 6 5 5 1 1 10 0 1 1 1 3 8 1 2 1 3 19 7 2 2 4 0 5 2 1 5 1 9 1 2 7 12 2 2 2 1 0 10 7 6 0 0 0 2 1 16 12 0 5 1 1 0 0 1 0 0 1 1 5 0 1 10 1 3 4 6 4 1 3 23 1 3 0 4 9 12 9 15 11 7 0 0 1 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Connectez-vous pour commenter.

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by