Clear Filters
Clear Filters

How to resolve Horzcat error (narrowed it down to one value in the for loop) and output for loop correctly.

1 view (last 30 days)
I have the following code. What I want the inner loop to do is calculate DipoleParam = x,y,z,mx,my,mz and put it into the row of a new matrix...i.e. by the end of the inner loop, I should have a 3*7 matrix. Then by the end of the outer loop, I should have two of these 3*7 matrices.
But I keep getting a horzcat error primarily because my c value is always showing as a 1*1...rather than a 1*n....I am just trying to see what the best way to resolve this error is.
Q = 2
Nini = 3
for q = 1:Q
for n = 1:Nini
Range = 2*pi*(rand(n,1));
Radius = 20*sqrt(rand(n,1));
x = 0 + Radius.*cos(Range);
y = 0 + Radius.*sin(Range);
z = -5 +(10)*rand(n,1);
mx = 2000*rand(n,1);
my = 2000*rand(n,1);
mz = 2000*rand(n,1);
c = 5-n
DipoleParam = [Dipolex Dipoley z Mx My Mz c]
end
end

Answers (1)

David Hill
David Hill on 3 Sep 2020
Seems to be lots of problems with your code. I likely don't understand everything.
for q = 1:2%I don't see a need for an inner loop
Range = 2*pi*(rand(3,1));%generates 3x1 array
Radius = 20*sqrt(rand(3,1));
x = 0 + Radius.*cos(Range);
y = 0 + Radius.*sin(Range);
z = -5 +(10)*rand(3,1);
mx = 2000*rand(n,1);%3x1 array
my = 2000*rand(n,1);
mz = 2000*rand(n,1);
c = 5-n%I have no ideal what c is suppose to be but it must be a 3x1 array
DipoleParam{q} = [Dipolex Dipoley z Mx My Mz c]%what are Dipolex and Dipoley? not defined anywhere...Is Mx, My, Mz suppose to be mx,my,mz?
end

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!