Keeping values in a matrix in a nested loop
11 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello, don't think my problem has been answered..i'm relatively new to MATLAB.
Basically i want to create matrix where each row is repeated the number of times as the value in the 3rd column for each row. i have set that part up in a loop and it works, what i'm having trouble with is keeping the data each time the loop iterates. There's a problem with the indexes but i can't quite place it for matrix 'B'. Error= Subscripted assignment dimension mismatch. Here's part of my code with the Loops:
%G1 is the original matrix (3457x3)
% N is a matrix with all the values in column 3 of G1
for g1= 1:length(G1);
g2= G1(g1,[1 2 3]); %g2 = the row of G1 corresponding to the index
for n=1:length(N);
n1= N(n);
g4= intersect(g2, n1); %g4 gets value i need to repmat with
if isempty(g4); %sometimes g4 doesn't match g2
continue
else
end
jg= find(ismember(G1,g2,'rows')); %finds all indexes in big matrix with same row pattern.
g4= round(g4, integer);
g5= repmat(g2, size(jg),1);
Save= repmat(g5, g4,1);
B(1:length(Save), end+1)= Save ;
n=n+1;
end
g1=g1+1;
end
I have used B(1:length(Save), end+1) before in a nested loop and that seemed to work. i have also tried B(n,:) and subsequent other solutions from other questions. Any suggestions would be great!
EDIT: I have solved the problem but i had to pre-allocate a 3 dimensional matrix, if anyone knows how to keep a two dimensional matrix the problem still stands, here is the revised code:
clear all
S= load('extra_spike_mat');
St= S.extra_spike_mat;
index=1;
integer= 0;
St1= St;
St1(any(St==0,2),:)= []; %removes rows with zeros
%repeat row the number of times as spike rate
N= St1(:,3); %spike rates
St3= St1(:,[1 2]); %amp and freqs
index= 1;
max_nrep= max(N);
max_nrep= round(max_nrep);
nrows= numel(St1);
B=zeros(nrows, max_nrep,3);
Nunique = unique(N);
for st1= 1:length(St1);
St2= St1(st1,3);
nrep= round(St2);
Vect= repmat(St1(st1,:), nrep,1);
%size(Vect)
B(st1, 1:nrep, :) = Vect;
% index=index+1;
st1=st1+1;
end
All i have to do is remove the zeros, but being a noob this is harder for me to deal with.
0 Commenti
Risposte (0)
Vedere anche
Categorie
Scopri di più su Multidimensional Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!