error in selecting data

조회 수: 7 (최근 30일)
FIR
FIR 2011년 11월 15일
i have a data of 2 columns
d= [12] 'A'
[23] 'B'
[18] 'H'
d1=[23 45]
out = [d1, d(ismember(d(:,1),d1(:,1),'rows'),1:end)]
if i do this i get error
Input must be cell arrays of strings
please help

채택된 답변

Sven
Sven 2011년 11월 15일
Hi FIR,
The error comes because the ismember() function expects either a matrix of numbers or a cell array of strings. Since d is a cell, you provided a cell array of numbers which is unfortunately invalid. Try converting to numbers like the following:
dMask = ismember(cell2mat(d(:,1)), d1);
Secondly, you were trying to concat d1 (which is a 1x2 matrix) with various rows of a cell (ie, an nx2 cell):
out = [d1, d(dMask,:)]
This won't work on two counts:
1. Cells won't concat with matrices - one of them needs to be converted.
2. d1 is 1x2, which won't concat if your result has more than 1 rows.
------
So, I've updated my answer as below from the comments you made. Firstly, we have your inputs d and d1 as follows:
d = {12 'A'; 23 'B'; 18 'H'; 12 'P'};
d1 = [23 45;18 10];
And what you actually want to do is concat d1 with its corresponding indices into d. Note that this means you're asking which entries in the first column of d1 are members of the first column of d... Note that this is the opposite ordering from your original question, but it seems more logical. Try the following two lines, I think that they answer your question:
[dMask, indices] = ismember(d1(:,1), cell2mat(d(:,1)));
[num2cell(d1(dMask,:)) d(indices,2)];
Does this help?
  댓글 수: 12
Sven
Sven 2011년 11월 16일
I'm a bit lost on what you want to do here, and it seems quite a different topic to the original question. I suggest hitting "accept" to the original question, and then asking a new question specifically about "how to format rows columns for display". If you can make a short example showing exactly the input you have and the output you want, I'm sure it will get a clear answer.
FIR
FIR 2011년 11월 16일
Sven u have not answered to my question

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by