필터 지우기
필터 지우기

Create cell array using values from an index of another array

조회 수: 5 (최근 30일)
Mikel
Mikel 2022년 9월 28일
답변: Yash 2023년 9월 7일
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
dpb
dpb 2022년 9월 28일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Yash
Yash 2023년 9월 7일
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.
For more details on various kinds of indexing, you can refer the documentation here.
I hope this helps you address the issue.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by