필터 지우기
필터 지우기

Create sperate matrices for all unique values in the 3rd column of a cell array

조회 수: 4 (최근 30일)
I have a cell array of 666x3, in this array the first and second columns have completely unique values, but the 3rd column has identifying values. I want to make a new array for each identifying value. Keep in mind there are many different values in column 3 as well and i dont want to manually input them, I want it to be done automatically so any data I use, the same result will be shown. Thank you!
Example:
initialArray =
column1 column2 column3
240.747574230792 298.790970549445 127
239.818737359994 297.862133678646 127
241.676411101591 297.862133678646 126
240.283155795393 296.933296807848 126
235.174553006003 296.004459937050 125
233.781297699806 295.540041501651 125
220.777581508630 269.997027554699 125
required:
array1=
column1 column2 column3
240.747574230792 298.790970549445 127
239.818737359994 297.862133678646 127
array2=
column1 column2 column3
241.676411101591 297.862133678646 126
240.283155795393 296.933296807848 126
array3=
column1 column2 column3
235.174553006003 296.004459937050 125
233.781297699806 295.540041501651 125
220.777581508630 269.997027554699 125

채택된 답변

Matt J
Matt J 2021년 6월 2일
편집: Matt J 2021년 6월 2일
Well, I won't talk about creating separate arrays because it's bad, but here's what you could do which wouldn't be bad:
initialArray = {
240.747574230792 298.790970549445 127
239.818737359994 297.862133678646 127
241.676411101591 297.862133678646 126
240.283155795393 296.933296807848 126
235.174553006003 296.004459937050 125
233.781297699806 295.540041501651 125
220.777581508630 269.997027554699 125};
initialArray=cell2mat(initialArray);
[~,~,G]=unique(initialArray(:,3),'stable');
array = splitapply(@(x){x}, initialArray, G);
It's essentially what you asked for, except array is a cell array whose contents are the array chunks that you asked for.
array{:}
ans = 2×3
240.7476 298.7910 127.0000 239.8187 297.8621 127.0000
ans = 2×3
241.6764 297.8621 126.0000 240.2832 296.9333 126.0000
ans = 3×3
235.1746 296.0045 125.0000 233.7813 295.5400 125.0000 220.7776 269.9970 125.0000
  댓글 수: 3
Talha Majeed
Talha Majeed 2021년 6월 2일
You're right it does, I was looking at the wrong thing, thanks for you help!

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by