This question already has answers here:
How do I get the index of the smallest element in an array in matlab?
(2 answers)
Closed 8 years ago.
I want to match a time to the minimum value of TEMP found in an array that I'm reading into a struct from a netcdf file in a loop (that's doing a lot more stuff - no I don't want to get rid of the loop). I have the snctools so that's what I'm using for netcdfs.
Here's my current relevant lines of code:
%Get the netcdf file with file_string loading into MATLAB
nc=netcdf(file_string);
%Work out the number of files I need to loop through
[files]=dir('*.nc');
max1=length(files);
for d1=1:max1
%extract the TEMP 1-D array
B1=nc{'TEMP'}(:)
%assign to value
dat.TEMP_min(d1)= min(B1);
end
Now there is another variable of the same length called 'TIMES'. If min(B1)=10.5 and is the nth element of B1 then I want to locate the nth element of TIMES and save it as dat.TEMP_min_TIME. How would I go about this?
Please provide enough notes on any code so that a novice can understand it.
You can just use inside your loop (sorry for pseudocode, not sure about your definitions of n and TIMES)
[M,I] = min(B1);
dat.TEMP_min(d1)= M;
if (I == n)
dat.TEMP_min_TIME = nc1{'TIMES'}(I); %
end
Related
This question already has answers here:
MATLAB, how to change loop index inside for loops [duplicate]
(2 answers)
Change for loop index variable inside the loop
(4 answers)
Closed 1 year ago.
I am triying to write a code which automatically checks the input data with a bounded range and removes the ones outside this boundary. I have written the following code:
LANDA_E_4=landa;
for i=1:m
if i>m
break
elseif LANDA_T_2(i)<0.2021e+03 || LANDA_T_2(i)>1.3317e+03
LANDA_T_2(i)=[];
i=i-1;
end
The problem here is that the "i" does not update within the loop. Consider the first element is not within the range, so it gets removed. now the loop should check the new first element which is the previous second element (before removing the first one) but the "i" whitin the loop is still 2. I can't update "i".
Thank you in advance
The problem with your approach is that at each iteration of the loop i gets set to the number of that iteration, even if you modify it in between. (And besides taht it is bad practice to use i as a variable, as by default it is set to the imaginar unit 1i.) To make your approach work you would have to use a while loop.
But it is even easier to use logical indexing instead and avoid loops like so:
% bougs data
m = 100;
LANDA_T_2=rand(1, m)*2e3;
% remove loops
LANDA_T_2(LANDA_T_2<0.2021e+03 | LANDA_T_2>1.3317e+03) = [];
disp(size(LANDA_T_2))
Try it online!
This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 6 years ago.
When I want to access a specific element of a matrix, I use indexing with parentheses:
m = calc_stuff(...);
x = m(index1, index2);
However, I often want to do that in one line of code, like this:
x = calc_stuff(...)(index1, index2);
How can I express it?
A specific example:
m = cumsum(rand(10,4));
x = m(10, 1);
The above script calculates some sums of random variables, and then I take one example value out of the result matrix.
How could I write it as one line? The following doesn't work:
x = cumsum(rand(10,4))(10, 1);
Error: ()-indexing must appear last in an index expression.
Here, I want a general syntax, which is applicable for any calculation, not necessarily involving random variables.
You may want to check out the "Functional Programming Constructs" on the FileExchange).
Especially the file paren.m does what you need. So you would write
x = paren( cumsum(rand(10,4)), 10, 1 );
Perhaps not as elegant as the direct "()"notation, but that is not supported in MATLAB in the way you would like to use it.
This question already has answers here:
The meaning of colon operator in MATLAB
(2 answers)
Closed 6 years ago.
I'm new to Matlab, and I'm trying to unscrabble the code here. In particular, I'm wondering what this line does:
ti = (Series{1}(2, i) - L*Dt):Dt:(Series{1}(2, i)-Dt);
In particular, what does the colon do? I found this explanation:
But to me this doesn't tell me what it's doing here. Similarly, I'm not even clear on what Series{1}(2, i) - L*Dt produces. I get that normally the answer would be 'try it out', but I don't have access to Matlab and so would appreciate any comments or advice.
Thank you.
Curly braces are used for cell arrays, a data-structure that can hold any type. In this case, I think it's a safe guess that Series{1} contains a matrix, so Series{1}(2,i) is just a specific entry. Writing this out step-by-step might be the easiest to understand:
A = Series{1} % get the matrix
t0 = A(2,i) - L*Dt;
tN = A(2, i) - Dt;
ti = t0:Dt:tN; % create a time-series from time t0 to tN, with step-size Dt
in the linked code it says Each cell is a 2xT matrix. First row contains the values and the second row contains SORTED time stamps. The first time series is the target time series which is predicted.
Series{1}(2, i) fetches the i-th time stamp ((2,i) means), in the first time series ({1} means) (which i guess is used as a reference frame), and let's think that it is a certain given number T0.
The second step of the code is to establish a time array, starting from T0 - L*Dt and stops at T0 - *Dt. The length of each increment is Dt.
It's same as The meaning of colon operator in MATLAB as pointed out by #TroyHaskin . But i think it's the cell array that makes it less easy to understand.
This question already has answers here:
Save loop data in vector
(2 answers)
Closed 7 years ago.
I want to save the following for-loop results in a new matrix using Matlab, how can I do that or any other suggestions?
Where X is a 5467-by-513 matrix , id is a 143-by-1 vector and wkno is a 44-by-1 vector
for i=1:size(id,1);
for j=1:size(wkno,1);
tst= X(:,1)==id(i) & X(:,2)==wkno(j);
M=mean(X(tst,:));
end
end
Just make sure you actually save the things to a matrix instead of a scalar-variable, i.e. add the subscript indices to the variable you're saving to:
for ii=1:size(id,1);
for jj=1:size(wkno,1);
tst(ii,jj)= X(:,1)==id(ii,1) & X(:,2)==wkno(jj,1);
M(ii,jj)=mean(X(tst,:));
end
end
Not that I refrained from using i and j as a variable, since this is a bad idea. I added the ,1 to id and wkno, to make sure you use them as column variables. This is a good habit to get into, because single indices will go wrong when you have a multi-dimensional array.
This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 9 years ago.
I was asking myself if there was a quicker way to do this in matlab :
Imagine we have a 10x2 vector V and we want to use the x dimension (number of lines, here 10) in a function or do whatever we want with it. The way I usually do it is this :
[x y]=size(V);
function(x)
But would it be possible to make it differently? Soemething like
function(size(V)(1))
Thanks for your help !
MATLAB's size can take a second input argument, indicating the dimension of which you would like to know the size. The output is scalar in that case:
x = size(V,1);
y = size(V,2);
See help size for more details.