How to process a large matrix of size 28x28x11684? - matlab

I'm having 11684 matrices each of size 28x28. So the variable a has size 28x28x11684. Now i would like to do sorting them using a for loop on each matrix of 28x28 and store it in a variable z. Here is my code
for i=1:11684
z=sort(a(:,:,i));
end
When i run the code, it is giving me the variable z of size 28x28. But i want the variable z to be of size 28x28x11684. Plese help me.

You don't need any loop at all. sort is directly applicable on multi-dimensional arrays as well.
z = sort(a);
This is it!

Remember that sort will sort the columns. This is how you do it:
a=rand(28,28,55);
z=a*0;
for i=1:size(a,3)
z(:,:,i)=sort(a(:,:,i));
end

Related

Parallel computation with cell array

A,B,C: cell arrays of size 100 x 1.
Each cell of A is a matrix. All have same size.
B and C contain vectors.
I need to create a cell array D of size 100 x 1. Serial code will look something like:
for i=1:100
D{i}=my_func(A{i},B{i},C{i});
end
where my_func is a function that takes input of a matrix and vectors, producing a vector.
I want to use parfor (or spmd) to make things faster. However, A has large size so I don't want to broadcast A to all workers. Is there a way to do this efficiently, given that my_func takes sometime? If anyone can give me a small example, I will appreciate it.
It looks like your cell arrays are all sliced variables. This means they will not be broadcast to all workers, only those segments each worker needs.
Thus, you can safely replace the for with parfor:
parfor i = 1:100
D{i} = my_func(A{i}, B{i}, C{i});
end

Reshape structure into a matrix without for loop

I have a structure (data) which consist of 322 cells a 296(features)*2000(timepoints). I want a matrix per timepoint which consists of trials^features^timepoints (322 *296*2000). What I am currently doing and what also works fine is using a for-loop:
for k=1:size(data.trial{1,1},2)
for i= 1:length(data.trialinfo)
between=data.trial{1,i}';
data(i,:,k)=between(k,:);
end
end
Can anyone think of a faster way to do that? Because it takes ages as the matrix increases.
Thanks!
Carlos
Try:
data2 = permute(reshape([data.trial{:}],296,2000,322),[3,1,2]);
The issue with reshape is that it thinks in columns, so you need to permute afterwords (ie. 3D transpose), since that's how you set up the output variable in your loop. I used concatenation instead of cell2mat for speed.
I assumed this worked as sample data:
for ii= 1:322
data.trial{1,ii} = rand(296,2000);
end

peak finder for multidimensional array

I'm trying the following and is not working. Could someone help me on this?
A=rand(1,4,5);
peak_num=zeros(5,4);
for w=1:5
peak_num(w,:)=peakfinder(A(1,1:4,w))
end
peak_num;
in this case the vector of peaks found for each w has a different size.
Thanks
I haven't really taken a look at the internals of the peakfinder function but if you make sure that it does not output a vector with more than 4 elements this is a workaround:
A=rand(1,4,5);
peak_num=zeros(5,4);
for w=1:5
temp = peakfinder(A(1,1:4,w));
peak_num(w, 1:length(temp) ) = temp
end
peak_num;
It sets the first elements to the return values and keeps the others being zero.

complex matlab for loop with matrix

I don't understand this piece of code of a for loop in matlab, I know that loops in matlab usually look like: for ii=1:2:100 so that it starts in 1 until 100 and in each iteration you add 2.
But here I've got this condition in the loop and I don't get what it does:
for ii=[1:w:rd(1)-w-border, rd(1)-w-border+1],
...
end;
w and border are integers passed as arguments and rd is size of the image/matrix (rd = size(image);)
Can someone explain me how for loops work in matlab with this kind of condition?
Thanks in advance.
For loop in matlab can execute statements for a defined set of index values:
For example, the following code will display all the element in the set [1,5,8,17]:
for s = [1,5,8,17]
disp(s)
end
Your code for ii=[1:w:rd(1)-w-border, rd(1)-w-border+1] is similar.
Its just like a set 1:w:rd(1)-w-border with an additional element rd(1)-w-border+1.
Its like writing this set [1,2,3,4,5,8] as [1:1:5, 8]
I hope its clear now.
the for argument is a vector. the loop iterator ii takes one value for the vector for each iteration of the loop. As you mentioned, the vector can be equally spaced one like 1:2:100. But it can also be arbitrary, for example for ii = [4,6,1,8] ....
In you case the for argument vector is partly "equally spaced" vector: 1:w:rd(1)-w-border plus another element rd(1)-border+1.

Referring to coordinates of a 3d matrix in Matlab

In Matlab I'm trying to find points in a 3d matrix whose coordinates are smaller than some function.
If these coordinates are equal to some functions than I can write:
A(some_function1,some_function2,some_function3)=2;
But what if I want to do something like:
A(<some_function1,<some_function2,<some_function3)=2;
This isn't working - so what is the other way of finding such points without using "for" loop? Unfortunately with "for" loop my code takes a lot of time to compute. Thank you for your help!
How about something along the lines of
A( ceil(min(some_function1,size(A,1))),...
ceil(min(some_function2,size(A,2))),...
ceil(min(some_function3,size(A,3))) );
This will cap the indicies to the end of each array dimension
You can just use regular indexing to achieve this:
A(1:floor(some_function1),1:floor(some_function2),1:floor(some_function3)) = 2;
assuming you check / ensure that floor(some_function*) is smaller than the dimensions of A
Try:
A(1:size(A,1)<some_function1, 1:size(A,2)<some_function2, 1:size(A,3)<some_function3) = 2
I hope I got your question correctly.