Create cell array using values from an index of another array
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
Having an 2D array called x1x2 with some values in it, I created a cell array called ROIx1x2 that saves values from x1x2 if they meet some conditions. Having ROIx1x2 with values of x1x2 located in different cells, I need to create cell array F that will save values in the same disposition as ROIx1x2, but based on the position of the elements of the first 2D array.
I mean:
I know that elements in fintd matches the same position at x1x2(1,:). I need to save values if fintd into F in the same disposition as ROIx1x2 knowing this relationship.
my guess is that I need somehow get the indexes of x1x2 and then match this same indexes with the fintd array, but I dont know how to do it.
Here my code:
%% INIT
clear variables;
close all;
clc;
%%
load('points.mat', 'x1x2','X1','X2','PDLUT','fintd');
%
m=1;
%
x1=x1x2(1,:);
x2=x1x2(2,:);
cond=cell(192,1);
ROIx1x2=cell(192,1);
%%
for indX1=1:16
for indX2=1:12
cond{m}=x1x2(x1x2(1,:)>X1(indX1) & x1x2(1,:)<X1(indX1+1) & x1x2(2,:)>X2(indX2) & x1x2(2,:)<X2(indX2+1));
ROIx1x2{m}(1,:)=cond{m}(1,1:2:end-1);
ROIx1x2{m}(2,:)=cond{m}(1,2:2:end);
% fintd has the same elements as x1x2(1,:), I need to save it into F in
% the same way as ROIx1x2. Dont know how to manage indexes
% to create this new cell array
F{m}= fintd(:,:);
m= m+1;
end
end
1 Kommentar
dpb
am 28 Sep. 2022
This is one of those where trying to follow the verbal description leaves one lost -- give us a simple input and then the expected output that comes from it to illustrate.
If the point is to simply make a second array with the same size as another and with elements in it that are in known locations; it's probably possible that simply using a logical addressing array will do it all in one step.
Antworten (1)
Yash
am 7 Sep. 2023
Hi Mikel,
I understood that you are interested in creating an array from another array based on certain conditions.
To accomplish this in MATLAB, you can utilize logical indexing. Here's an example code snippet:
% The main array
x1x2 = [1 3 5 2 7 3 1 7 3];
% Logical array: element is 1 if the condition is true and 0 if the condition is false
ROI_indexes = x1x2<5
% Extract from the main array using the logical array.
ROIx1x2 = x1x2(ROI_indexes)
In this example, the array "ROIx1x2" contains elements from "x1x2" that are greater than 5.
I hope this helps you address the issue.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!