In matlab I have two vectors, ind and ind3. ind = [1 2 3 4 5] and I want to define ind3 based on ind such that I want ind(3), ind(4) and ind(5) to be ind3(1) and ind3(2) and ind3(3). so that ind3 = [ind(3) ind(4) ind(5)] but for some reason I can't do this. I thought it would be simple to do using nested for loops but it doesn't really work.
for i=3:5
for n=1:3
ind3(n,:) = ind(i,:);
end
end
By going through the for-loops logically I know why the output is wrong.. but I don't get how else to do it? Am I being stupid and missing something really simple?!
I know its probably a simple answer but can anybody help??
Thanks.
If you want ind3 = [ind(3) ind(4) ind(5)] and you want to do it in a loop you just need a single loop. Additionally, since you're dealing with vectors you just have one indexing variable.
for n=1:3
ind3(n) = ind(n + 2);
end
Maybe I'm misunderstanding your question but is this what u want:
ind3=ind(3:5)
First of all, you said that your arrays are one dimensional (they are not matrices), and in your code by calling ind3(n,:) or ind(i,:) you treat them like 2 dimensional arrays.
As long as everything is 1 dimensional here, you need just one for loop:
for i=3:5
ind3(i)=ind(i-2);
end
Explanation: here i=3,4,5. For i=3 you assign ind3[3]=ind[1], for i=4: ind3[4]=ind[2], for i=5: ind3[5]=ind[3].
Or you can simply call ind3=ind(3:5)
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
I have two beginner's questions:
(1) I want to reshape an array, but dimensions come from a vector which can be a variable. For example,
A = ones(120,1)
b = [2,3,4,5]
I can write
C = reshape(A,2,3,4,5)
But in case b can vary, I want something like
C = reshape(A,b)
This code works in Matlab. Is there an analog in Julia?
(2) I want to slice a high-dimensional array, while keeping the dimensions flexible. In the example above, I fix the last dimension:
C[:,:,:,1]
C[:,:,:,2]
etc. The problem is to find an efficient way: For an array of any dimensions, I can always fix the last dimension and extract values.
Any help will be highly appreciated!
(1) C = reshape(A,b...)
(2) EllipsisNotation.jl provides a .. operator, so C[..,1] does what you want.
And there is C[ntuple(x->:, ndims(C)-1)..., 1] for (2) if you don't want to install a package.
I'm trying to work with arrays, but I can't seem to make one that works for my data. I have 14 matrices I would like to put in an array, but I can't figure out the way to do it without manually writing c(m1,m2,m3...) to put in all of them
this is what i tried:
m_list <- mget(paste0("well_", 0:13)) ###to make a list of all my matrices
a <- array(c(m_list),
dim = c(7338, 15, 14))
but when I try to look at the array I created something is not right with it cause I try to call for one value, like this:
print(a[1,4,2])
but I get entire columns.
I assume the error is in the list of matrices. Please help
An answer to your question is that you should use do.call(c, m_list) instead of c(m_list). (Take a couple of small matrices and try to see what c(m_list) and c(m1, m2) return.)
Also you might want to think some more whether working with an array is better than working with a list and, more importantly, how you could avoid having multiple matrices in the first place and instead to directly read/define them as a list or an array.
You can simply use unlist inside your array function call instead of c.
a = array(unlist(m_list), dim = c(dim(m_list[[1]]), length(m_list)))
Some reproducible data:
m1 = matrix(1:5, 5, 5)
m2 = matrix(5:1, 5, 5)
m_list = list(m1, m2)
Consider the following code snippet
for i = 1:100
Yi= x(i:i + 3); % i in Yi is not an index but subscript,
% x is some array having sufficient values
i = i + 3
end
Basically I want that each time the for loop runs the subscript changes from 1 to 2, 3, ..., 100. SO in effect after 100 iterations I will be having 100 arrays, starting with Y1 to Y100.
What could be the simplest way to implement this in MATLAB?
UPDATE
This is to be run 15 times
Y1 = 64;
fft_x = 2 * abs(Y1(5));
For simplicity I have taken constant inputs.
Now I am trying to use cell based on Marc's answer:
Y1 = cell(15,1);
fft_x = cell(15,1);
for i = 1:15
Y1{i,1} = 64;
fft_x{i,1} = 2 * abs(Y1(5));
end
I think I need to do some changes in abs(). Please suggest.
It is impossible to make variably-named variables in matlab. The common solution is to use a cell array for Y:
Y=cell(100,1);
for i =1:100
Y{i,1}= x(i:i+3);
i=i+3;
end
Note that the line i=i+3 inside the for-loop has no effect. You can just remove it.
Y=cell(100,1);
for i =1:100
Y{i,1}= x(i:i+3);
end
It is possible to make variably-named variables in matlab. If you really want this do something like this:
for i = 1:4:100
eval(['Y', num2str((i+3)/4), '=x(i:i+3);']);
end
How you organize your indexing depends on what you plan to do with x of course...
Yes, you can dynamically name variables. However, it's almost never a good idea and there are much better/safer/faster alternatives, e.g. cell arrays as demonstrated by #Marc Claesen.
Look at the assignin function (and the related eval). You could do what asked for with:
for i = 1:100
assignin('caller',['Y' int2str(i)],rand(1,i))
end
Another related function is genvarname. Don't use these unless you really need them.
I am looking for something like list comprehensions in matlab however I couldnt find anything like this in the documentary.
In python it would be something like
A=[i/50 for i in range(50)]
Matlab is very fond of 'vectorizing'. You would write your example as:
A = (0:49) ./ 50
Matlab hates loops and therefore list comprehension. That said, take a look at the arrayfun function.
You can do:
(1:50)/50
Or for something more general, you can do:
f=#(x) (x/50);
arrayfun(f,1:50)
No, Matlab does not have list comprehensions. You really don't need it, as the focus should be on array-level computations:
A = (1:50) / 50
Matlab can work with arrays directly, making list comprehension less useful
If what you're trying to do is as trivial as the sample, you could simply do a scalar divide:
A = (0:50) ./ 50
There are several ways to generate a list in Matlab that goes from 0 to 49/50 in increments of 1/50
A = (0:49)/50
B = 0:1/50:49/50
C = linspace(0,49/50,50)
EDIT As Sam Roberts pointed out in the comments, even though all of these lists should be equivalent, the numerical results are different due to floating-point errors. For example:
max(abs(A-B))
ans =
1.1102e-16
This doesn't work help with your numerical example but for the special case of strings there is the compose function that does the same thing as a list comprehension of the form:
s = [f"Label_{i}" for i in range(1, 6)]
Example:
str = compose("Label_%d", 1:5)
Result:
str =
1×5 string array
"Label_1" "Label_2" "Label_3" "Label_4" "Label_5"