Getting specific values from 3D matrix

Hi all, I have a 1200x1200x100 matrix and I'm trying to select/crop it using x indicies and y indicies I have found of interest. The all 4 lists of coordinates are 1x34 matrix. I should be getting something like 150x160x100 but running into problems using loops. Thank you for any help!
for N=1:34
xdata(N) = data(X1coordinates(N):X2coordinates(N),Y1coordinates(N):Y2coordinates(N),100);
end

7 comentarios

Luna
Luna el 9 de En. de 2019
Could you please share the example data( a matrix you want to crop(smaller example)), your x,y coordinates and desired output for them? What are the 4 lists?
Adam Danz
Adam Danz el 9 de En. de 2019
Editada: Adam Danz el 9 de En. de 2019
Please proved an example or your data. The numbers in your question aren't clear.
Dylan George
Dylan George el 9 de En. de 2019
Editada: Dylan George el 9 de En. de 2019
Hi, thanks for your time. The 4 lists are x1 x2 y1 and y2, each of them are 1x34 matricies. They contain the indicies on the x and y axis id like to crop eg. 2 nd row for each of these lists is 135 , 139 , 560, 567 respectively. Therefore id like a 4x7x100 area of interest for this row. The end goal is to find the area of interests (34 areas thisyouyou ) using the original 'data' 1200x1200x100 to create a time frame i.e a plot of the intensity values in the 'data' against the number of frames in the z axitimes (100). Hope this clears it up a little. Thank
Guillaume
Guillaume el 9 de En. de 2019
And how do you want to store these frames? If they're all the same size, as a 4D matrix? If they're of different size, as a cell array? Something else?
Dylan George
Dylan George el 9 de En. de 2019
Editada: Dylan George el 9 de En. de 2019
I don't really know whats best to be honest. They will be different sizes. I have been suggested a cell array with this line of code, but it only works for the first row (gives 4x3x100).
xdata=cell(1,size(X1coordinates,2)); % preallocate
for i = 1:size(X1coordinates,2)
xdata{i} = data(X1coordinates(:,i):X2coordinates(:,i),Yicoordinates(:,i):Y2coordinates(:,i),:) ;
end
celldisp(xdata)
Bob Thompson
Bob Thompson el 9 de En. de 2019
What do you mean by "it only works for the first row."?
Dylan George
Dylan George el 10 de En. de 2019
The first row - meaning x1coordinates(1,1) :coordinates(1,1) and y1coordinates(1,1):y2coordinates(1,1) . Not all 34 rows which I need.

Iniciar sesión para comentar.

 Respuesta aceptada

Guillaume
Guillaume el 10 de En. de 2019
If the areas are different sizes, then you don't have a choice but storing them in a cell array. The code you have written is the correct approach. I suspect that it only works for the first row because your X1Coordinates is a column vector whereas your code assumes it's a row vector. That's easily fixed by iterating over the rows instead of columns or even better, by writing code that makes no assumption about the shape of the inputs and simply iterate over all the elements:
xdata = cell(size(X1coordinates)); %result is same shape and number of elements as X1coordinates
for i = 1 : numel(xdata) %iterate over all the elements
xdata{i} = data(X1coordinates{i}:X2coordinates{i}, Y1coordinates{i}:Y2coordinates{i}, :);
end
Or you can replace the whole lot by a single arrayfun:
xdata = arrayfun(@(x1, x2, y1, y2) data(x1:x2, y1:y2, :), X1coordinates, X2coordinates, Y1coordinates, Y2coordinates, 'UniformOutput', false);
Note that if your original code is correct, the name Xncoordinates, and Yncoordinates are a bit misleading, since your X is along the rows and Y along the columns which is not the standard convention.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 9 de En. de 2019

Comentada:

el 10 de En. de 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by