Fail to multiply column using .*
Show older comments
I have google and try to find the answer whole day to make below code.ONLY to make multiplication.
I already change from .* to * and try another method but doesnt work.
please teach me how.im so stress now because im very new to matlab.
thank you.
clc
clear
%Create Combination
[a,b,c,d,e]=ndgrid(1:2,1:2,1:2,1:2,1:2);
data=[a(:),b(:),c(:),d(:),e(:)];
PV={'PV1',1,2;...
'PV2',3,4};
Batt={'B1',5,6;...
'B2',7,8};
MPPT={'MPPT1',9,10;...
'MPPT2',11,12};
Inverter={'Inv1',13,14;...
'Inv2',15,16};
DieselGenerator={'DG1',17,18;...
'DG2',19,20};
ExpandData1=[PV(data(:,1),:), Batt(data(:,2),:), MPPT(data(:,3),:), Inverter(data(:,4),:), DieselGenerator(data(:,5),:)]
T1 =ExpandData1(:,3)
T2 =ExpandData1(:,3)
T3 = T1.*T2 %<<<----------------error out
Undefined operator '.*' for input arguments of type 'cell'.
Error in Entahla (line 28)
T3 = T1.*T2
Accepted Answer
More Answers (2)
Scott MacKenzie
on 30 Apr 2021
I'm not sure what the "big picture" is here, but if you are just trying multipy T1 times T2, then the fundamental problem is that T1 and T2 are cell arrays and the opertation you are attempting is not valid -- as stated in the error message. So, convert to numeric arrays first, then multiply:
T1 =ExpandData1(:,3)
T2 =ExpandData1(:,3)
t1Array = cell2mat(T1);
t2Array = cell2mat(T2);
T3 = t1Array .* t2Array
% T3 = T1.*T2 %<<<----------------error out
With this, your code executes without an error and generates the following output:
T3 =
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
6 Comments
Shahid Said
on 30 Apr 2021
Scott MacKenzie
on 30 Apr 2021
Edited: Scott MacKenzie
on 30 Apr 2021
I don't get any error when I run your code with the modification I suggested.
I'm not sure what you mean by "Let's say T1 is not a number". T1 is a cell array of numbers. It becomes a numeric array via cell2array. If you are changing T1 or T2 to hold, say, character or string arrays, that fundamentally changes the problem. In this case, multiplication doesn't make sense in the first place.
BTW, you are aware that T1 and T2 are the same, right?
Shahid Said
on 30 Apr 2021
Scott MacKenzie
on 30 Apr 2021
You've change the definition of T1 from T1 =ExpandData1(:,3), in your original question, to T1 =ExpandData1(:,1). This changes everything. Now, T1 contains text labels, so multiplcation doesn't make sense. Sounds like you want to do some sort of look up: pull a value from T2 or T3 based on some code that is looked up in T1. Is that right?
BTW, look carefully at your code. The error you're getting now is pretty obvious:

Shahid Said
on 1 May 2021
Edited: Image Analyst
on 1 May 2021
Image Analyst
on 1 May 2021
Edited: Image Analyst
on 1 May 2021
MATLAB is case sensitive, so T1Array was never defined. It is not the same as t1Array.
And you cannot convert T1 to numbers because it's character arrays. Let's say you were to convert 'PV', 'PV2', etc. to numbers. What would you expect to get???
Did you try
T1 =ExpandData1(:,1) % is text 'PV','PV2'
T2 =ExpandData1(:,2) % is number 1,3,1,3
T3 =ExpandData1(:,3) % is number 2,4,2
%t1Array = cell2mat(T1); <<_----error here because T1 is not number
t2Array = cell2mat(T2);
t3Array = cell2mat(T3);
Table =[T1Array,T2Array,T3Array]
Shahid Said
on 1 May 2021
0 votes
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!