Saving data from a double for loop
15 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(1 个)
Jan
2021-3-9
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!