Can I perform subtraction using cell arrays?

Hi all,
I have 2 sets of numbers which i would like to subtract them and compare their values and perform a counter loop.
Is there any function I can make use of to do that? Thanks
for example I have these 2 sets of numbers which I need to subtract each value and compare:
filteredAreaSample =
3594 3627 3626 3625 3610
filteredAreaStudent =
3648 3621 3613 3620 3626

1 commentaire

Matt J
Matt J le 14 Oct 2012
Modifié(e) : Matt J le 14 Oct 2012
It really doesn't make sense for you to maintain data like this in cell arrays. Data like this can be held in simple matrices, making them very simple (and CPU optimized) to subtract
Differences = filteredAreaSample - filteredAreaStudent
Cell arrays are meant mainly for situations where you have arrays of different sizes or types, e.g.,
mycell={rand(5), eye(3),'dog','cat'},
or when you want to perform comma-separated list operations, e.g.,
[A,B,C,D]=deal(mycell{:});

Connectez-vous pour commenter.

 Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 14 Oct 2012
Modifié(e) : Azzi Abdelmalek le 14 Oct 2012
A ={ 3594 3627 3626 3625 3610}
B ={ 3648 3621 3613 3620 3626}
res=cellfun(@(x,y) y-x,A,B)

10 commentaires

Yao
Yao le 14 Oct 2012
Hi Sir,
I got this error "Function name must be a string."
How do I troubleshoot it?
Azzi Abdelmalek
Azzi Abdelmalek le 14 Oct 2012
Modifié(e) : Azzi Abdelmalek le 14 Oct 2012
did you copy and past the code?, if not, what did you do? or show us a sample of your data
Hi Sir,
This is my code:
>> Sample1 = imread('Sample1.jpg'); >> Sample1 = img_preprocess_sample(Sample1);
filteredAreaSample =
3594 3627 3626 3625 3610
>> Student1 = imread('Student1.jpg'); >> Student1 = img_preprocess_student(Student1);
filteredAreaStudent =
3648 3621 3613 3620 3626
>> filterAreaSample = {3594 3627 3626 3625 3610};
>> filterAreaStudent = {3648 3621 3613 3620 3626};
>> Difference = cellfun(@(x,y) y-x,filterAreaSample,filterAreaStudent)
Azzi Abdelmalek
Azzi Abdelmalek le 14 Oct 2012
Modifié(e) : Azzi Abdelmalek le 14 Oct 2012
It's wierd, I tested your code, it works. Now to find what is the problem try:
filterAreaSample = {3594 3627 3626 3625 3610};
filterAreaStudent = {3648 3621 3613 3620 3626};
Difference=cell2mat(filterAreaStudent)-cell2mat(filterAreaSample )
what did you get?
Hi Sir, thanks for your help, I got the results.
Just to check, I need to convert the values into a matrix before I can find the difference?
Because for my next step I will need to compare the difference and I was told to convert it to a cell array to compare in a loop
Here is a sample of my code:
I have values stored from DiffQ1 to DiffQ20 I need to compare the values
Difference = 600
Results = 0
if DiffQ1 > Difference
Results = Results + 0
else
Results = Results + 1
end;
to
if DiffQ20 > Difference
Results = Results + 0
else
Results = Results + 1
end
Azzi Abdelmalek
Azzi Abdelmalek le 14 Oct 2012
Yao , what are the sizes of DiffQ1, ...? can you post your full code?
Hi Sir, based on the earlier code, I got the results as shown in the code below. Now I will need to store the results into DiffQ1 to DiffQ20 and compare them with MaxDiff. I will need to compare them 20 times and get the final show the final results.
Difference = cell2mat(filteredAreaStudent) - cell2mat(filteredAreaSample)
Difference =
Columns 1 through 12
54 -6 -13 -5 16 -23 9 46 -36 33 65 5
Columns 13 through 20
-40 72 45 -8 -49 -31 -15 -8
DifffQ1 = 54;
DiffQ2 = -6;
DiffQ3 = -13;
DiffQ4 = -5;
DiffQ5 = 16;
DiffQ6 = -23;
DiffQ7 = 9;
DiffQ8 = 46;
DiffQ9 = -36;
DiffQ10 = 33;
DiffQ11 = 65;
DiffQ12 = 5;
DiffQ13 = -40;
DiffQ14 = 72;
DiffQ15 = 45;
DiffQ16 = -8;
DiffQ17 = -49;
DiffQ18 = -31;
DiffQ19 = -15;
DiffQ20 = -8;
MaxDifference = 100
Results = 0
if DiffQ1 > Difference
Results = Results + 0
else
Results = Results + 1
end;
Azzi Abdelmalek
Azzi Abdelmalek le 14 Oct 2012
Modifié(e) : Azzi Abdelmalek le 14 Oct 2012
you don't need to do it for each value!
res=[ 54 -6 -13 -5 16 -23 9 46 -36 33 65 5 -40 72 45 -8 -49 -31 -15 -8]
out=res>MaxDifference
Hi sir, i tried your method and it works.
Thank you so much for your help.
Lets say if I want to build a counter that says if res is larger then 100; add 1 if res is smaller then 100 add 0, how should I do it? In this case, the output are all zeros, so I should show my results as 20. Thanks again for your help
out = Difference >100
out =
Columns 1 through 12
0 0 0 0 0 0 0 0 0 0 0 0
Columns 13 through 20
0 0 0 0 0 0 0 0
Azzi Abdelmalek
Azzi Abdelmalek le 14 Oct 2012
Modifié(e) : Azzi Abdelmalek le 14 Oct 2012
Can you be more clear, you have your array Difference, what test do you want to do? And I think you should accept the answer that correspond to your expectation then post another question.

Connectez-vous pour commenter.

Plus de réponses (1)

Wayne King
Wayne King le 13 Oct 2012
Modifié(e) : Wayne King le 13 Oct 2012
out = cellfun(@minus,filteredAreaSample,filteredAreaStudent);

1 commentaire

Yao
Yao le 14 Oct 2012
Hi Sir,
I got this error "Function name must be a string."

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by