Loop with in a loop - arrays

Is it possible to index an array in a loop with in a loop (Matlab)?
So I'm indexing a 3d matrix with 3 parameter:
For instance:
A(1,1,:)
A(1,2,:)
...
A(1,9,:)
then I want the code to jump to the next element of the first prameter:
A(2,1,:)
A(2,2,:)
...
A(2,9,:)
And so on until I finish with all the elements of the first parameter.
Now I have a :
for j=1:27
for i=1:9
X(j,i,:)= f(j,i,:)
end
end
But I'm only getting a row of results when it should be a matrix of 27x9x:
Do anyone know how to make the second loop (i) runs 9 times for the first value of j and then move to the second value of j and run 9 times i and so on?
Thanks!

use squeeze(); something like this:
for j=1:27
for i=1:9
tmp= squeeze(f(j,i,:));
X(j,i,:) = tmp;
end
end

Related

Why doesn't my code compare the first and last number?

I have to compare up to 5 numbers in my script. That also works quite well. However, my first and last number are not compared. What's the problem in my code?
local loaded = game.Workspace.TrommelValue.Value
local randomkugel = {}
for i = 0,loaded-1 do
randomkugel[i] = math.random(1,6)
for index = 0, i+1 do
if i ~= index then
if randomkugel[i]==randomkugel[index] then
randomkugel[i]=math.random(1,6)
index = 0
end
end
end
end
Thank you for helping me!
I don't know what game.Workspace.trommelValue.value is, but let's assume its a positive number.
First iteration
-- assign a random integer from [1-6] to `randomkugel[0]`
randomkugel[0] = math.random(1,6)
Now you run the inner loop the first time. The first cycle is skipped as i == index.
The second cylcle is skipped because randomkugel[i] is the random number and randomkugel[index] is nil
Second iteration of the outer loop. i is 1, random[1] is assigned a random value from [1-6].
Inner loop:
first run i is 1, index is 0 so the first if statement is entered.
randomkugel[1] may randomly equal randomkugel[0]. In that case you would assign a new random value to randomkugel[1] and set index to 0 which does not have any effect.
In case both values are not equal nothing happens.
second cycle of the inner loop, i is still 1, index is 2.
As there is no randomkugel[2] this cycle does nothing.
You will always skip the last cycle of the inner loop as you're comparing vs nil every time.
So your inner loop is effectively
for index = 0, i do
if i ~= index then
if randomkugel[i]==randomkugel[index] then
randomkugel[i]=math.random(1,6)
end
end
end
I guess your greatest misconception here is that you can somehow reset the loop counting variable index inside the loop body.
Any change to index is only valid after that change within the current iteration.

Are my arrays incompatible?

I am trying to loop through an array that contains split strings, done via this line:
repHolder() = Split(rep, ",")
That's all fine and good, however, when I try to loop through this repHolder() array via a for loop, I am met each time with a subscript out of range error.
This makes no sense to me. When I step through the array it fails on the first element every time; this line:
If repHolder(j) = counter Then
I tried setting j to 0 and 1, both of which failed on the first sequence of the loop. This suggests to me because the array doesn't have a defined size; that I cannot loop through it this way, but that still makes little sense to me as it is still filled with elements.
Here is the entire code block of what I am trying to do:
Dim repHolder() As String
Dim strHolder() As String
Dim counter As Variant
Dim j As Integer
For Each rep In repNames()
repHolder() = Split(rep, ",")
Next rep
For Each rangeCell In repComboRange
k = 1
Do
If rangeCell.Value = repCombos(k) Then 'At this point, if rangecell = repcombos(k)
Range(rangeCell, rangeCell.End(xlToRight)).Copy
strHolder() = Split(rangeCell.Value, "/")
For Each counter In strHolder()
Stop
For j = 1 To 17
If repHolder(j) = counter Then
You are looping through repNames() and setting this new array via split (over and over again for each repName element...)
For Each rep In repNames()
repHolder() = Split(rep, ",")
Next rep
Every iteration of this loop resets repHolder() to a split of the rep element dropping whatever values were just set in that array in the previous iteration. So once it's done only the last element of RepNames() has been split into the repHolder() array.
For instance, if RepNames() looks like:
Element 0: "james,linda,mahesh,bob"
Element 1: "rajesh,sam,barb,carrie"
Element 2: ""
Then after all this iterating your repHolder array is going to be empty because there is nothing in the final element.
Stick a breakpoint (F9) on you For Each rangeCell In repComboRange line and look at your Locals pane in VBE. Check out the values that are stored in your repHolder() array at that point in time. I suspect there will be nothing in there.
The other oddball here is that you are looping 1 through 17. repHolder() will be a 0-based array so that should be 0 through 16. But... even that is nonsense since this really only makes sense as a For Each loop (or to use the uBound(repHolder) to determine how many times to loop:
For Each counter In strHolder()
Stop
For each repHolderElem in repHolder
If repHolderElem = counter Then
....
Next repHolderElem

Matlab, save output in array , loop

I want to create an array that can store the outputs each time that doing a loop. I think the problem is because in a every new iteration the numbers starts counting from the beginning so it stores only the last iteration! In each iteration the output is an array(7x3) so in total I have to have (28,3).But I tried a lot and i AM GETTING AN ARRAY (28,3) all with zeros except the last 7 rows.
Thank you very much
You can see the code below:
for t=1:ncell % in my case I have 4 cells
ti=sort(T,2)
tt= sort(Cell{t}.ExBot,2)
tq= sort(Cell{t}.ExTop,2)
te= sort(Cell{t}.ExBT,2)
%k=0
z=0
cc=[]
%%%%% for exbottom
I=ones(size(ti,1),1);
for j=1:size(tt,1)
for i=1:size(ti,1)
if tt(j,:)==ti(i,:)
k=k+1 ;
%c(k,:)=[ti(j,:), ti(j+1,:)]
I(i)=0;
cc(k,:)=Y(i,:);
cc(size(tt,1)+1,:)=cc(1,:)
else
end
end
end
end
Although more info would help as mentioned in the comments, from the information you've given, the problem is most likely in setting cc to empty when you start processing each cell.
cc=[];
On exiting the outermost loop you will only have results for the last iteration.
On a related note you may want to use isequal or all for the comparison of vectors i.e. if isequal(tt(j,:),ti(i,:))

overwritten values in loops: Matlab

The code below , when I execute it, ub,u, p, q arrays are shown like 101 times since ii=101 in the code. What I mean is the loop does not work. Could you look at it please
ii=101;
dt=0.0001;
t = 0:dt:1000;
dx=0.01; %step size
pi=4.*atan(1);
fik=0.4;
H1=0.5;
H1D=0;
A=0;
AD=0.1;
ADinit=AD;
c1b=0.5;
c2b=1-c1b;
dc=0.001;
for i=1:ii;
x=(i-1)*dx;
fikness=fik*sin(pi*x);
ub1(i)=(c1b-H1D*(x-0.5)+AD/2*(x-0.5)^2)/(H1-0.5*fikness-A*(x-0.5))
ub2(i)=(c2b+H1D*(x-0.5)-AD/2*(x-0.5)^2)/(1-H1+0.5*fikness+A*(x-0.5))
end
c1=c1b+dc;
c2=1-c1;
for i=1:ii;
x=(i-1)*dx;
fikness=fik*sin(pi*x);
u1(i)=(c1-H1D*(x-0.5)+AD/2*(x-0.5)^2)./(H1-0.5*fikness-A*(x-0.5))
u2(i)=(c2+H1D*(x-0.5)-AD/2*(x-0.5)^2)./(1-H1+0.5*fikness+A*(x-0.5))
end
p1(1)=0.5*(1-u1(1)^2);
q1(1)=0;
p2(1)=0.5*(1-u2(1)^2);
q2(1)=0;
for i=2:ii
q1(i)=q1(i-1)-dx*(u1(i-1)-ub1(i-1))/dt
p1(i)=0.5*(1-u1(i)^2)+q1(i)
end
for i=2:ii;
q2(i)=q2(i-1)-dx*(u2(i-1)-ub2(i-1))/dt
p2(i)=0.5*(1-u2(i)^2)+q2(i)
end
ub1,ub2,u1,u2,p1,p2,q1,q2 =1*101, all of them have one row 101 column How can I access the whole arrays of ub1,ub2,u1,u2,p1,p2,q1,q2 ??
the command prompt will show all calculations which are not followed by a semicolon. even if you are assigning one element of an array, it will show the whole array.
p(1)=1
this shows all of p, not just the first element. put semicolon at the end of each assignment statement or calculation like
p2(i)=0.5*(1-u2(i)^2)+q2(i);
then at the end of the code write
u1
u2
this should show the final vectors

How do I account for the extra elements at the end of an array if it is shorter in the next iteration of a nested loop

I've got a nested for loop, in the inner loop I've got an array that will change size and value in each iteration,e.g;
a=[ 2 3 4]
and in the next iteration it will be :
a=[9 5]
but the result of my code is :
a=[9 5 4]
a(3) is the problem, it is from the previous iteration and I don't want it,so what should I do?
I do not know how to write my code here cause it contains lots of functions and you wont understand it!?
but it's sth like this:
for j=1: 5
%l is the length of row in cell array(a) that varies from one row to another
for i=1:l
dn=a{j,i};
spp(t)=dn(1)
end
targ{j,1}=spp;
end
spp is the problem here
Insert a clear command to delete the temporary variable (once spp have three elements, it never goes back to a 2 elements vector unless you clear it or declare it).
...
targ{j,1}=spp;
clear spp;
...
Alternatively, you can code the matlab-way by declaring your variable before it gets populated. In this situation, there is no need for a clear command.
for j=1:5
%l is the length of row in cell array(a) that varies from one row to another
spp = zeros(1,l);
for i=1:l
...

Resources