How to concatenate the data in each iteration in the same variable?
Mostra commenti meno recenti
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 Commenti
M
il 21 Nov 2023
Dyuman Joshi
il 21 Nov 2023
I'll get back to you in some time.
Dyuman Joshi
il 21 Nov 2023
Modificato: Dyuman Joshi
il 21 Nov 2023
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.
M
il 21 Nov 2023
Spostato: Dyuman Joshi
il 21 Nov 2023
Dyuman Joshi
il 21 Nov 2023
Modificato: Dyuman Joshi
il 21 Nov 2023
"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
"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)
randperm(10,4)
M
il 21 Nov 2023
Spostato: Dyuman Joshi
il 21 Nov 2023
M
il 21 Nov 2023
Spostato: Dyuman Joshi
il 21 Nov 2023
Walter Roberson
il 21 Nov 2023
Spostato: Dyuman Joshi
il 21 Nov 2023
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);
Dyuman Joshi
il 21 Nov 2023
Modificato: Dyuman Joshi
il 21 Nov 2023
"nope, I need the indices to do matching with the images!!"
Just to confirm, you need 4 outputs (images_TRO_pressing, images_TRO_slip, images_TRO_noise and labels_TRO) with values from all iterations?
M
il 21 Nov 2023
Walter Roberson
il 21 Nov 2023
Modificato: Walter Roberson
il 21 Nov 2023
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
il 21 Nov 2023
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
Risposte (0)
Categorie
Scopri di più su Creating and Concatenating Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!