How to apply operation (< or >=) on cell array?

조회 수: 3 (최근 30일)
Muhammad Hayyan Bin Shahid
Muhammad Hayyan Bin Shahid 2022년 4월 6일
댓글: MJFcoNaN 2022년 4월 7일
I have exported the excel sheet on MATLAB and now I want to apply condition on each cell.
Condition: If each value in cell is less tha 150 so the answer should be "0" else "1".
The cell array dimension is 44x4.
I want the outout in the same array form too.
MY CODE
clc;
clear all;
DATA = readcell('testing_Data.xlsx');
DATA_A = readcell('testing_Data.xlsx','Range','A1:A44')
DATA_B = readcell('testing_Data.xlsx','Range','B1:B44');
DATA_C = readcell('testing_Data.xlsx','Range','C1:C44');
DATA_G = readcell('testing_Data.xlsx','Range','D1:D44');
A_OUTPUT = DATA_A(find(~cellfun(@(x)any(x<150, 'all'), DATA_A)))
Using this above code the output result I am getting is 27x1 matrix.
A_OUTPUT is eleminating the cells that are less than 150, however instead of eliminating thoses cell, I want to replace "0" on their place. And for the cell who's value is greater than 150 should be replaced by "1".

답변 (1개)

MJFcoNaN
MJFcoNaN 2022년 4월 6일
A little modified code may work:
ind_less = cellfun(@(x)any(x<150, 'all'), DATA_A);
A_OUTPUT = NaN(length(DATA_A), 1);
A_OUTPUT(ind_less)=0;
A_OUTPUT(~ind_less)=1;
  댓글 수: 2
Muhammad Hayyan Bin Shahid
Muhammad Hayyan Bin Shahid 2022년 4월 7일
Thank you so much. It worked!!
CAn you also guide me how to represent the results as one array?
Like I want the coloumn A B C and G to be in 44x 4 cell array.
MJFcoNaN
MJFcoNaN 2022년 4월 7일
Why is there a cell array? The A_OUTPUT is a numerical vector, isn't it?

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by