Saving data from a double for loop

Hi
I am struggling with writing a line of code for saving generated variables from regionprops. I have a set of frames (let's say 10) on which I obtain various regions of interest (ROIs) (value varies from 0 to 10 for example). I obtain properties which are variables for each ROI on each frame and would like to generate an array which saves in the first column the number of the frame, the second the number of the ROI (so if frame 1 had 10 ROIs, ROI = 11 would be from frame 2) and then in the last columns some unique variables obtained from regionprops fn.
clear
clc
close all
Frame =[1:10];
Matrix = [];
for Frame = 1:10
for ROI = 1:10
Matrix(ROI,:) = [Frame;ROI];
end
end
The output is Matrix which is a 10x2 double but I would expect a 100x2 double for the first column would contain in the first 10 rows the number 1, and in the second column the number 1 to 10. The 11th to 20th would be the Frame = 2, etc..
Should I maybe concantenate a unique array after the first two for loops? Or is there a way of allowing the next full loop to write the data in without overwriting the previous saved data?
Thanks a lot!

回答(1 个)

Frame =[1:10]; % No need for the concatenation operator [] here
% But "Frame" is overwritten by this at all:
for Frame = 1:10 % Here "Frame" is overwritten.
% So you can omit the above definition.
There is no way to magically concatenate arrays. Simply do this directly:
Matrix = zeros(10, 10, 2):
for Frame = 1:10
for ROI = 1:10
Matrix(Frame, ROI, :) = [Frame; ROI];
end
end
Maybe you want to reshape the array finally:
Matrix = reshape(Matrix, 100, 2)

3 个评论

Thanks a lot, Jan.
I'm not sure if I wasn't clear in my explanation before, but the resulting matrix has now in the first column the Frame numbers from 1-10 and all the first ROIs from each frame. (I changed the ROI from 1:100 as well).
I would like to now try and introduce the two variables that are calculated for each ROI, let's call them A1 and A2. So the resulting Matrix should be of a size 1000x4 for example. Below a snippet of my code for a very large zero matrix due to the actual size of the matrix (determined by the total amount of ROIs in the code) not being known prior and a variable number of frames defined by numFrame.
Matrix = zeros(numFrame,500000,4);
for Frame = 1:numFrame
for ROI = 1:length(regionprops(Frame),'A1','A2'); %example
%calculation for A1(ROI)
%calculation for A2(ROI)
%resulting in unique variable A1 and A2 from ROI for loop inside Frame loop
Matrix(Frame, ROI, A1, A2, :) = [Frame, ROI, A1, A2];
end
end
%calculation to determine total amount of ROIs resulting in totalROI
Matrix = Matrix(:,totalROI);
The following line of code in my script gives me an error
"Index in position 4 is invalid. Array indices must be positive integers or logical values.
Error in Script (line X)
Matrix(Frame,ROI,A1,A2,:) = [Frame;ROI;A1;A2];"
I hope this makes somehow sense. The final result of the matrix obtained would be one where I have a matrix of size totalROIx4 (from the four parameters: Frame, ROI, A1 and A2) which I can then make into a table or .csv file.
Thanks a lot again for the help!
What is the contents of A1 and A2?
A1 is a calculated value using regionprops (for example Area of the ROI using regionprops fn). The regionprops fn returns a table for each ROI and the calculated value. I then use A1 to calculate A2. So A1 and A2 are unique values for each ROI in each frame.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by