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
Related
I want to assign part of a matrix into another matrix using a for loop in MATLAB. I've tried different ways but none of them worked. I want to know what's wrong with this one:
fullGrid = complex(zeros(FFTLen, numSym, numTx),zeros(FFTLen, numSym, numTx));
for i=0:(numSym/2)-1
for j=0:(FFTLen/2)-1
A(i,j)=[fullGrid(i,j)];
end
end
You made a very basic mistake. The index position in a matrix/array in
Matlab starts from 1 and not 0. So replace all the for loops from 1 to
required length.
Corrected code is given below.
fullGrid = complex(zeros(FFTLen, numSym, numTx),zeros(FFTLen, numSym, numTx));
for i=1:(numSym/2)-1
for j=1:(FFTLen/2)-1
A(i,j)=[fullGrid(i,j)];
end
end
Hi I'm trying to write simple for loop with if loop to only calculate the mean of an array column wise (so i end up with just one row array as a mean), except column number 1051 and 1552. Here the input array has 2151 columns and 12 rows. The result in mean array should be 2149 columns and 12 rows. Here is the code I wrote
function specmeanex, a
m=make_array(2151,1)
for i=0,2150,1 do begin
if (i ne 1051) or (i ne 1552) then begin
m[i,0]=mean(a[i,*])
endif
endfor
plot,m
return,m
end
How can i either assign the empty spaces to 0 using an else? Can anyone please help me out here since I'm quite new to IDL
You could just add an else clause like this:
if (i ne 1051) or (i ne 1552) then begin
m[i,0]=mean(a[i,*])
endif else m[i,0] = 0.0
But, I recommend changing the code a bit to remove the for loop. IDL can be very efficient in vectorized operations that operate on arrays all in one statement, but slow when looping over elements of an array. Here, you are looping over the columns, which is not so bad, but could be made more IDL-like.
Try this code:
function mg_column_mean_exclude, a, exclude_columns
compile_opt strictarr
m = mean(a, dimension=2)
m[exclude_columns] = 0.0
return, m
end
Then you can call it similar to your previous routine, except you would pass in the excluded columns too:
m = mg_column_mean(a, [1051, 1552])
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,:))
This is my first time posting so i hope you can help me. I am trying to write a function in matlab.
I have laded data from a file into a cell array. First column contains statements and the second contains T for true og F for false. I now want to split this array into a cell array with the statements and a logical vector with 1 for True and -1 for false.
I use the fgetl within a loop to read all the lines into the cellarray
Try to write it a bit more neatly next time, and consider including a small example.
Here is what you seem to be looking for:
Suppose you have a matrix M and want to split that into M_true and M_false
M = {1,'T';
22,'F';
333,'T'}
idx_T=strcmp(M(:,2),'T')
M_true = M(idx_T,1)
M_false = M(~idx_T,1)
In a program I'm writing, I've created an allocated, final product array AFT(n,92). In my output I would like present each row as its own table, 5 columns wide.
So in this case, it would be n individual tables of 19 rows X 5 columns with only 2 values on the final row. I attempted doing this as a do loop as shown in the code snip below, but the output comes out as just one long column. I'm not sure where to go from here.
DO i=1,n
WRITE(4,800) t(i), ' HHMM LDT' !Writes the table header using an array which holds the corresponding time value
800 FORMAT(14, A9)
DO j=1,92
WRITE(4,900) AFT(i,j)
900 FORMAT(5ES23.14)
END DO
END DO
I believe this is happening because the write command is performed for each j individually due to the use of a loop, but my inexperience with FORTRAN is leading me to a blank when I try to come up another approach.
Yes, each write statement produces one line of text output. If you want multiple items to be included in the same output record, you have to include them in the write statement. If you want to include portions of an array, you can use techniques such as:
do i=1, N
write (*, *) (array (i,j), j=1, 5)
end do
or
do i=1, N
write (*, *) array (i, 1:5)
end do
The first is using implied do loops, the second array sections.