weird problem while copying a double value from an array into another.
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi, I have an array like this:
magnitude=[1,1.25,1.5,1.75];
I want to copy the values in it into another matrix with this:
row=1;
inj_duration = 192;
inj_starts_at=[108,120,132,144];
for node=1:G.NodeCount
for startTime=1:4
inj_sc(row,:)=[node , magnitude(startTime) , inj_starts_at(startTime) , inj_duration];
row=row+1;
end
end
And the result became integer in the inj_sc's second column.
However, if I use something like this, no problem appears:
inj_sc=[randi(G.NodeCount,Ns,1), max_inj_conc*rand(Ns,1), randi(48,Ns,1)+inj_start_time, randi(inj_duration,Ns,1)];
The result becomes [int, double, int, int] and this is the what I want. But I can't use random numbers, I have an order for my project.
Also, If I do the following in the command line, still no problem:
inj_sc(1,2)=0.34
So, what is the problem with my nesting loops?
0 Commenti
Risposta accettata
Guillaume
il 22 Nov 2016
"The result becomes [int, double, int, int]". No, the result is [double, double, double, double] with some of these double having integral values. You may think it is nitpicking, but it is most likely the reason for your issue.
You cannot have a mix of different types in a matrix. If one of the type is integer (that is one of int8, uint8, int16, uint16, int32, uint32, int64 or uint64) then all the other values get converted to that type.
I suspect that G.NodeCount is an integer type, and therefore, so is node. As a result, so is inj_sc. You can check with
class(G.NodeCount)
To fix:
for node = 1 : double(G.NodeCount)
or
inj_sc(row,:)=[double(node) , magnitude(startTime) , inj_starts_at(startTime) , inj_duration];
However, note that the loop is unnecessary:
inj_sc = [repelem(1:double(G.nodeCount), numel(magnitude));
repmat([magnitude; inj_starts_at], 1, G.nodeCount);
repmat(inj_duration, 1, double(G.nodeCount) * numel(magnitude))] .'
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!