Extract multiple data points from matrix

29 ビュー (過去 30 日間)
Dylan George
Dylan George 2019 年 1 月 9 日
コメント済み: alexander ridgers 2022 年 5 月 31 日
Hi everyone, anyone know how to make a loop, or automate this somehow, got 30+ lines of this! Thanks
xdata1 = data([extendedX1coordinates(:,1):X2coordinates(:,1)],[Y1coordinates(:,1):Y2coordinates(:,1)],:) ;
xdata2 = data([extendedX1coordinates(:,2):X2coordinates(:,2)],[Y1coordinates(:,2):Y2coordinates(:,2)],:) ;
xdata3 = data([extendedX1coordinates(:,3):X2coordinates(:,3)],[Y1coordinates(:,3):Y2coordinates(:,3)],:) ;
xdata4 = data([extendedX1coordinates(:,4):X2coordinates(:,4)],[Y1coordinates(:,4):Y2coordinates(:,4)],:) ;
xdata5 = data([extendedX1coordinates(:,5):X2coordinates(:,5)],[Y1coordinates(:,5):Y2coordinates(:,5)],:) ;
xdata6 = data([extendedX1coordinates(:,6):X2coordinates(:,6)],[Y1coordinates(:,6):Y2coordinates(:,6)],:) ;

回答 (4 件)

madhan ravi
madhan ravi 2019 年 1 月 9 日
xdata=cell(1,size(extendedXicoordinates,2)); % preallocate
for i = 1:size(extendedXicoordinates,2)
xdata{i} = data(extendedXicoordinates(:,i):X2coordinates(:,i),Yicoordinates(:,i):Y2coordinates(:,i),:) ;
end
celldisp(xdata)
  1 件のコメント
Dylan George
Dylan George 2019 年 1 月 9 日
thanks for your help so far madhan.! It only gives me 3x4x100 though :/ I should have been clearer in my question. the x1,y1,x2,y2 are multiple coordinates, matricies of 1x34.

サインインしてコメントする。


Matt Tearle
Matt Tearle 2019 年 1 月 9 日
It might help to know a bit more about what you're actually trying to do. Given that you're indexing with (:,k), it seems that the variables XXXcoordinates are matrices...? But then you're extracting columns, so column:column would be weird and may not be doing what you want. So maybe those variables are vectors?
Then what are you going to do with the result? As Madhan points out, you don't want 30 uniquely named variables in your workspace. What's the end goal here?
  2 件のコメント
Dylan George
Dylan George 2019 年 1 月 9 日
Hi, Thanks for answering. Yeah the columns x1,y1,x2,y2 are all matricies (1x34) which means i need to find the difference between the two columns containing indicies and create a loop for it (1:34). The end goal is to crop the 3d matrix into 34 'areas' of interest so i want something like 120x120x100 to start with then I'd like to plot the intensity of these areas of interest individually against the z axis (100) to get 34 time frames. Thanks for your time
Matt Tearle
Matt Tearle 2019 年 1 月 16 日
OK, if the coordinates are row vector (1-by-34), then data(x1(:,i):x2(:,i)) is valid, although I'd still prefer to write it as data(x1(i):x2(i)) for clarity. I find the use of row, column indexing with the colon confusing when the result is a single index value.
But now I don't see why Madhan's code isn't doing what you want.
xdata{i} = data(x1(i):x2(i),y1(i):y2(i),:)
The ith cell of xdata will be a portion of data, with the rows and columns extracted being defined by x1, x2, y1, and y2. If the size of xdata{i} isn't what you'd expect, you'll need to look at the values of x1(i), x2(i), y1(i), and y2(i).
One thing to be wary of: if you think of x and y in our normal mathematical arrangement (x horizontal, y vertical), then as indices x would correspond to columns and y to rows. (So data(y1(i):y(2),x1(i):x2(i),:).)

サインインしてコメントする。


Dylan George
Dylan George 2019 年 1 月 9 日
thanks for your help so far madhan.! It only gives me 3x4x100 though :/

alexander ridgers
alexander ridgers 2022 年 5 月 30 日
編集済み: Image Analyst 2022 年 5 月 31 日
I'm having the same problem, and can explain it more clearly. If anyone has the solution I would be very grateful for them to reply please.
Dylan wants to pull out values X(1,1), X(2,2), X(3,3) from an array.
If he types in X(1:3,1:3) then he is returned a 3x3 matrix of values X(1,1), X(1,2), X(1,3);X(2,1),X(2,2),X(2,3);X(3,1),X(3,2),X(3,3).
Is there a way to pull out only X(1,1), X(2,2), X(3,3), without doing a loop such as:
Y=zeros(3,1);
for i=1:3
Y(i,1) = X(i,i)
end
  3 件のコメント
Matt Tearle
Matt Tearle 2022 年 5 月 31 日
I think what you're looking for is linear indexing. In your example, Y = X([1 5 9]) will get the elements you want.
alexander ridgers
alexander ridgers 2022 年 5 月 31 日
That’s brilliant; many thanks, Matt.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by