How to concatenate the data in each iteration in the same variable?

I am saving the output of the 1st iteration in the images_TRO_pressing , images_TRO_slip, images_TRO_noise, the size of each variabe is 120*120*300
In the other iterations how can I keep adding the data in the same varibales so I end up with size for each of them around 120*120*3600? as well same to the varibale labels_TRO with size 1*900 for one iteration
Note that the third dimension which is the number of matrices varies in each iteration not all iterations give 300.
for i = 1:12
pressing_labels = find(labels_all_Train1(i).labels == 0);
slip_labels = find(labels_all_Train1(i).labels == 1);
noise_labels = find(labels_all_Train1(i).labels == 3);
minimum = min([length(pressing_labels), length(slip_labels), length(noise_labels)]);
random_pressing = randsample(pressing_labels, minimum);
random_slip = randsample(slip_labels, minimum);
random_noise = randsample(noise_labels, minimum);
images_TRO_pressing = HM_all_Train1(i).TRO_heatmap(:, :, random_pressing);
images_TRO_slip = HM_all_Train1(i).TRO_heatmap(:, :, random_slip);
images_TRO_noise = HM_all_Train1(i).TRO_heatmap(:, :, random_noise);
labels_TRO = [labels_all_Train1(i).labels(random_pressing), labels_all_Train1(i).labels(random_slip), labels_all_Train1(i).labels(random_noise)];
end

16 Comments

Hi @Dyuman Joshi, Do you have any idea please? thanks
I'll get back to you in some time.
Hi @Dyuman Joshi , Please dont forget because I am stopped here, thanks
As for storing the data, You can preallocate a cell array to store arrays and then concatenate them after the for loop.
Also, you can use randperm instead of randsample. Stating this because the former does not require any toolbox, but the latter one does.
logical indexing is going to be faster.
I didnt get how this can return the index of a certin value ? doesnt it return the values itself?
you can preallocate a cell array to store arrays and then concatenate them after the for loop.
Could you please provide me with example because I tried it but it keeps only return the last iteration I dont know what is the error
you can use randperm
Can you please show me how can I use it to take a random sample with a certain number of samples from a vector, I didnt find this in the documentation
I appreciate your help, thanks
"Could you please provide me with example because I tried it but it keeps only return the last iteration I dont know what is the error"
Try this -
n=12;
labels_TRO = cell(n,1);
for i = 1:n
%Allocate data which needs to be accessed frequently to a variable
arr = labels_all_Train1(i).labels;
pressing_labels = nnz(arr == 0);
slip_labels = nnz(arr == 1);
noise_labels = nnz(arr == 3);
minimum = min([pressing_labels, slip_labels, noise_labels]);
random_pressing = randperm(pressing_labels, minimum);
random_slip = randperm(slip_labels, minimum);
random_noise = randperm(noise_labels, minimum);
images_TRO_pressing = HM_all_Train1(i).TRO_heatmap(:, :, random_pressing);
images_TRO_slip = HM_all_Train1(i).TRO_heatmap(:, :, random_slip);
images_TRO_noise = HM_all_Train1(i).TRO_heatmap(:, :, random_noise);
labels_TRO{i} = [arr(random_pressing), arr(random_slip), arr(random_noise)];
end
See Preallocation for more info.
"Can you please show me how can I use it to take a random sample with a certain number of samples from a vector, I didnt find this in the documentation"
%4 random numbers from 1 to 10
randsample(10,4)
ans = 4×1
9 3 6 5
randperm(10,4)
ans = 1×4
6 7 9 1
@Dyuman Joshi these 3 lines returen to me wrong indices
pressing_labels = nnz(arr == 0);
slip_labels = nnz(arr == 1);
noise_labels = nnz(arr == 3);
When I got back to "find"
I got the following error
Error using randperm
Inputs must be nonnegative scalar integers.
Also, as you only need the number of elements that satisfy the condition
nope, I need the indices to do matching with the images!!
See nnz doesnt return the indices!!
A = [1 0 2 3 1 2 4 3 1 4] ;
nnz(A == 0)
ans = 1
@Dyuman Joshi I tried to use the logical indexing as the following but I got this error:
pressing_labels = arr == 0;
slip_labels = arr == 1;
noise_labels =arr == 3;
Error using randperm
Inputs must be nonnegative scalar integers.
even with randsample I got empty array here with the logical indexing
labels_TRO{i} = [arr(random_pressing), arr(random_slip), arr(random_noise)];
You would not do something like randperm(pressing_labels, 3) .
Instead you would do something like
P = randperm(numel(pressing_labels), 3);
rand_PL = pressing_labels(P);
"nope, I need the indices to do matching with the images!!"
Yeah, my bad. I overlooked the fact that the indices are used as input to randsample as well.
Just to confirm, you need 4 outputs (images_TRO_pressing, images_TRO_slip, images_TRO_noise and labels_TRO) with values from all iterations?
@Walter Roberson your solution didnt work, I got the same error
pressing_labels = find(arr == 0);
slip_labels = find(arr == 1);
noise_labels = find(arr == 3);
N = 4; %number to be sampled
if numel(pressing_labels) < N; error('not enough pressing locations to sample'); end
if numel(slip_labels) < N; error('not enough slip locations to sample'); end
if numel(noise_labels) < N; error('not enough noise locations to sampe'); end
random_pressing = pressing_labels(randperm(numel(pressing_labels), N));
random_slip = slip_labels(randperm(numel(slip_labels), N));
random_noise = noise_labels(randperm(numel(noise_labels), N));
However you do not want to take arr(random_pressing) and so on -- you know that arr() at pressing locations is going to be 0 because that is how you chose those locations, according to what was in arr() there.
@Walter Roberson Thanks a lot, this is worked and thanks for @Dyuman Joshi
but I am wondering why the logical indexing instead of "find" didnt work, any suggestion please?
You have not been clear as to what your code is, or what your desired outcomes are.
If you are trying to find N locations each of which correspond to a particular arr value, then you have a few possibilities:
  • do NOT just take arr(LOGICAL MASK) since you already know what the arr values are at those locations because that is how you chose the locations
  • taking a random sub-sampling of a logical mask would get you are random number of elements selected, and would not tell you where they were selected from
  • using find() to locate the elements and taking a random selection from those indices works fine if what you want to know is the locations
  • You could create a vector 1:numel(arr) and index that vector at a logical matrix such as arr==0 ... but you would effectively just have duplicated the work of find() less efficiently

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

M
M
on 21 Nov 2023

Commented:

on 21 Nov 2023

Community Treasure Hunt

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

Start Hunting!