Referring to coordinates of a 3d matrix in Matlab - 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.

Related

How to process a large matrix of size 28x28x11684?

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

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.

Zero padding in Matlab

I have image, and I want to do up sampling. First of all I need to plug zeros between pixels such that [1,2,3] transforms to [1,0,2,0,3]. Can anyone tell me how to do it without using paddarray and without using for loops?
Thank you in advance!
Something like this?:
B=zeros(size(img)*2);
B(1:2:end,1:2:end)=img;
However there are ways of up-sampling in matlab without having the need of doing it by hand, for example interp2
You could also make use of MATLAB's way of dynamically allocating variables if you don't specify a number for an index into the array. By omitting indexing into certain locations in your array, MATLAB will fill in these values with zeroes by default. As such:
B(1:2:5) = 1:3
B =
1 0 2 0 3
V = [1,2,3];
padded(numel(V)*2) = 0;
padded(1:2:end) = V
And then just deal with the trailing zero if numel(V) was odd
There is a function upsample that does exactly this from Octave-Forge -- see docs on upsample.
Or you can look at the source of upsample to see what implements it. Are you opposed to using a package or a function?

Permutation of pages of a 3d array in matlab

I have a 3D array representing an image in MATLAB. I want to reverse the position of pages(in my case slices).
Let's assume the number of pages is N. I want to replace first page with Nth, second with (N-1)th and so on.. Is there any function to do it in matlab. Now I am using the code below, but I have to avoid nested for loops, that is why I am looking for a prepared function. Any help would be appreciated.
Thank you in advance
I = ones(size(Image,1),size(Image,2),size(Image,3));
k=1;
for n=size(Image,3):-1:1
I(:,:,k) = Image(:,:,n);
k = k+1;
end
You can simply
I = Image(:,:,end:-1:1);
Another possibility, which lets you use the same notation for flipping the array along any dimension:
I = flipdim(Image, 3); %// 3 is the dimension you want to flip along

Adding a dimension to a matrix in Matlab

I need to add a new matrix to a previously existant matrix, but on his dimension coordinate.
I know this is hard to understand, so let's see it on a example:
I've a matrix like this:
480x640x3
And I want to add the following one:
480x640x6
The result has be this: (6+3 = 9)
480x640x9
As you can see it adds but on the 3rd dimension.
For concatenating along higher dimensions, use the function CAT:
newMatrix = cat(3,matrix1,matrix2);
I would say that gnovice's answer is probably the best way to go, but you could do it this way too:
matrix1(:,:,4:9) = matrix2;