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.
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:
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
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 8 years ago.
E.g. I have the output of cov(A,B), which is a 2×2 matrix.
I want to select the element in position 2,1 of the matrix.
I can do this by blah = cov(A,B) and then select blah(1,2).
This isn't the most efficient way to do it though, and I'd prefer to do it in one line. Is there a way to do that?
You can try using getfield():
getfield(cov(A,B), {1,2})
The performance difference between this and what you have currently will likely be negligible, however. I personally would prefer just using that temporary variable.
<stealing brilliance from Amro>
You can also do this:
C = builtin('_paren', cov(A,B), 2, 1);
</stealing brilliance from Amro>
This question already has answers here:
Array of Matrices in MATLAB
(6 answers)
Closed 9 years ago.
I am new to programming and I was wondering if my question has a simple implementation. I have a bunch of matrices and I want a way to be able to store them, or be able to easily call them and do operations on them. For example, if I have 100 matrices, called, M1,M2,...M100; is there a way I can rename them so that if I want to call the nth matrix, I can just write M(nth)?
EDIT:
For example, if I want to add M1+M1, M1+M2, ...,M1+M100; I want to be able to write a loop something kind of like,
for i=1:100
AM(i)=M(1)+M(i)
end
Is this possible?
Use cell array
AM = cell(1,100);
and set it as
AM{i} = Mi;
then you can access it as
AM{i};
note the use of {} to access each element of the cell array AM, that is in turn a matrix