Saving data from a double for loop

14 Ansichten (letzte 30 Tage)
Filip Fedorowicz
Filip Fedorowicz am 9 Mär. 2021
Kommentiert: Filip Fedorowicz am 9 Mär. 2021
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!

Antworten (1)

Jan
Jan am 9 Mär. 2021
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 Kommentare
Jan
Jan am 9 Mär. 2021
What is the contents of A1 and A2?
Filip Fedorowicz
Filip Fedorowicz am 9 Mär. 2021
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.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by