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개)
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
Filip Fedorowicz
2021년 3월 9일
편집: Filip Fedorowicz
2021년 3월 9일
Jan
2021년 3월 9일
What is the contents of A1 and A2?
Filip Fedorowicz
2021년 3월 9일
카테고리
도움말 센터 및 File Exchange에서 Image Arithmetic에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!